Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6246657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:43:48+00:00 2026-05-24T12:43:48+00:00

I got a problem mapping oracle by hibernate I got these classes Stock.java package

  • 0

I got a problem mapping oracle by hibernate

I got these classes

Stock.java

    package com.mc.stock;

    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.Basic;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.SequenceGenerator;
    import javax.persistence.Table;
    import javax.persistence.UniqueConstraint;

    @Entity
    @Table(name = "stock", uniqueConstraints = {
        @UniqueConstraint(columnNames = "STOCK_NAME"),
        @UniqueConstraint(columnNames = "STOCK_CODE")})
    public class Stock implements java.io.Serializable {

        private Integer stockId;
        private String stockCode;
        private String stockName;
        private Set<StockDailyRecord> stockDailyRecords = new HashSet<StockDailyRecord>(
            0);

        public Stock() {
        }

        public Stock(String stockCode, String stockName) {
            this.stockCode = stockCode;
            this.stockName = stockName;
        }

        public Stock(String stockCode, String stockName,
            Set<StockDailyRecord> stockDailyRecords) {
            this.stockCode = stockCode;
            this.stockName = stockName;
            this.stockDailyRecords = stockDailyRecords;
        }

        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_stock_id")
        @SequenceGenerator(name = "seq_stock_id", sequenceName = "seq_stock_id", initialValue = 1, allocationSize = 1)
        @Basic(optional = false)
        @Column(name = "STOCK_ID", unique = true, nullable = false)
        public Integer getStockId() {
            return this.stockId;
        }

        public void setStockId(Integer stockId) {
            this.stockId = stockId;
        }

        @Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
        public String getStockCode() {
            return this.stockCode;
        }

        public void setStockCode(String stockCode) {
            this.stockCode = stockCode;
        }

        @Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
        public String getStockName() {
            return this.stockName;
        }

        public void setStockName(String stockName) {
            this.stockName = stockName;
        }

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "stock")
        public Set<StockDailyRecord> getStockDailyRecords() {
            return this.stockDailyRecords;
        }

        public void setStockDailyRecords(Set<StockDailyRecord> stockDailyRecords) {
            this.stockDailyRecords = stockDailyRecords;
        }
    }

StockDailyRecord.java

    package com.mc.stock;

    import java.util.Date;
    import javax.persistence.Basic;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.SequenceGenerator;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    import javax.persistence.UniqueConstraint;

    @Entity
    @Table(name = "stock_daily_record", uniqueConstraints = @UniqueConstraint(columnNames = "DATEX"))
    public class StockDailyRecord implements java.io.Serializable {

        private Integer recordId;
        private Stock stock;
        private Integer priceOpen;
        private Integer priceClose;
        private Integer priceChange;
        private Long volume;
        private Date date;

        public StockDailyRecord() {
        }

        public StockDailyRecord(Stock stock, Date date) {
            this.stock = stock;
            this.date = date;
        }

        public StockDailyRecord(Stock stock, Integer priceOpen, Integer priceClose,
                Integer priceChange, Long volume, Date date) {
            this.stock = stock;
            this.priceOpen = priceOpen;
            this.priceClose = priceClose;
            this.priceChange = priceChange;
            this.volume = volume;
            this.date = date;
        }



        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_daily_record")
        @SequenceGenerator(name = "seq_daily_record", sequenceName = "seq_daily_record", initialValue = 1, allocationSize = 1)
        @Basic(optional = false)
        @Column(name = "RECORD_ID", unique = true, nullable = false)
        public Integer getRecordId() {
            return this.recordId;
        }

        public void setRecordId(Integer recordId) {
            this.recordId = recordId;
        }

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "STOCK_ID", nullable = false)
        public Stock getStock() {
            return this.stock;
        }

        public void setStock(Stock stock) {
            this.stock = stock;
        }

        @Column(name = "PRICE_OPEN", precision = 6)
        public Integer getPriceOpen() {
            return this.priceOpen;
        }

        public void setPriceOpen(Integer priceOpen) {
            this.priceOpen = priceOpen;
        }

        @Column(name = "PRICE_CLOSE", precision = 6)
        public Integer getPriceClose() {
            return this.priceClose;
        }

        public void setPriceClose(Integer priceClose) {
            this.priceClose = priceClose;
        }

        @Column(name = "PRICE_CHANGE", precision = 6)
        public Integer getPriceChange() {
            return this.priceChange;
        }

        public void setPriceChange(Integer priceChange) {
            this.priceChange = priceChange;
        }

        @Column(name = "VOLUME")
        public Long getVolume() {
            return this.volume;
        }

        public void setVolume(Long volume) {
            this.volume = volume;
        }

        @Temporal(TemporalType.DATE)
        @Column(name = "DATEX", unique = true, nullable = false, length = 10)
        public Date getDate() {
            return this.date;
        }

        public void setDate(Date date) {
            this.date = date;
        }

    }

and when I try to run this test:

    package com.mc;

    import java.util.Date;

    import org.hibernate.Session;

    import com.mc.stock.Stock;
    import com.mc.stock.StockDailyRecord;
    import com.mc.util.HibernateUtil;

    public class App {

        public static void main(String[] args) {
            System.out.println("Hibernate one to many (Annotation)");
            Session session = HibernateUtil.getSessionFactory().openSession();

            session.beginTransaction();

            Stock stock = new Stock();
            stock.setStockCode("7052");
            stock.setStockName("PADINI");
            session.save(stock);


            StockDailyRecord stockDailyRecords = new StockDailyRecord();
            stockDailyRecords.setPriceOpen(new Integer("2"));
            stockDailyRecords.setPriceClose(new Integer("11"));
            stockDailyRecords.setPriceChange(new Integer("10"));
            stockDailyRecords.setVolume(30L);
            stockDailyRecords.setDate(new Date());

            stockDailyRecords.setStock(stock);


            stock.getStockDailyRecords().add(stockDailyRecords);

            session.save(stockDailyRecords);

            session.getTransaction().commit();
            System.out.println("Done");
        }
    }

I get this error:

2011-08-12_02:14:43.296 WARN  o.h.util.JDBCExceptionReporter 
    - SQL Error: 2291, SQLState: 23000
2011-08-12_02:14:43.296 ERROR o.h.util.JDBCExceptionReporter 
    - ORA-02291: integrity  constraint (HX.SYS_C004028) violated - parent key not found
2011-08-12_02:14:43.296 WARN  o.h.util.JDBCExceptionReporter 
    - SQL Error: 2291, SQLState: 23000
2011-08-12_02:14:43.296 ERROR o.h.util.JDBCExceptionReporter 
    - ORA-02291: integrity constraint (HX.SYS_C004028) violated - parent key not found

Exception in thread "main" org.hibernate.exception.ConstraintViolationException: 
    Could not execute JDBC batch update

I’m new to hibernate and I would thank any help on this

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:43:48+00:00Added an answer on May 24, 2026 at 12:43 pm

    You are not setting the contents of the Set from stock (setStockDailyRecords). I would try annotating setStockDailyRecords as

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "stock", inverse = "true")
    

    That means that the set is not directly created in the database, they retrieve it using the inverse relationship (through the FK in StockDailyRecord)


    UPDATED to answer MHERO’s first comment.

    Check http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html#collections-mapping for the inverse attribute.

    Other option that you have is to move save(stock) just before the session is closed (remember to set the Set of setStockDailyRecords with the appropiate values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a problem at saving my entity. MApping: ?xml version=1.0 encoding=utf-8 ?> <hibernate-mapping
Hi I got a problem with mapping my entities. I'm using JPA2 and Hibernate
I got the following mapping: <?xml version=1.0 encoding=utf-8 ?> <hibernate-mapping xmlns=urn:nhibernate-mapping-2.2 auto-import=false> <class name=Gate.Users.User,
I've got a problem with fluent nhibernate table per class hierarchy mapping. I've got
When I got something like this ERROR/AndroidRuntime(18677): Caused by: java.lang.NullPointerException ERROR/AndroidRuntime(18677): at com.companyname.a.a.a(Unknown Source)
I've got a problem publishing my current Project status. Mapping: <publishers> <xmllogger /><!-- Log
Hi I am using JPA2 with Hibernate implementation and I got a simple mapping
i got problem with my code and hopefully someone able to figure it out.
I've got problem with connecting to MySQL database on my freshly installed Windows 7
Got a problem with ADOMD.NET 8.0, SQL2008 and our app. It isn't giving us

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.