aspose file tools*
The moose likes Object Relational Mapping and the fly likes Saving OneToMany , foreign key not set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Saving OneToMany , foreign key not set" Watch "Saving OneToMany , foreign key not set" New topic
Author

Saving OneToMany , foreign key not set

Frank VanOor
Ranch Hand

Joined: Jun 18, 2004
Posts: 49
I've two tables: TaStock and TaStockPrice. Field tastockid in table TaStockPrice is the foreign key to table TaStock.







persisting taStock with Children



I read that when persisting a parent class, hibernate automatically persist the children of that class. But instead, the following exception occurs:

javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: ERROR: null value in column "tastockid" violates not-null constraint


Thanks in advance for help

Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
You have to generate the entity ids somehow. For example:

Frank VanOor
Ranch Hand

Joined: Jun 18, 2004
Posts: 49
Thanks for reply,

The Id's were generated by the DB, but I changed the code and added the annotation : @GeneratedValue(strategy = GenerationType.IDENTITY). Still the same problem.
Alexander Bondarev
Greenhorn

Joined: Jan 04, 2012
Posts: 12
You have to remove the class variable 'tastockid' from TaStockPrice. The foreign key column for the many-to-one relationship is defined in the JoinColumn annotation.
And the column 'tastockid' should be insertable.
Frank VanOor
Ranch Hand

Joined: Jun 18, 2004
Posts: 49
I've commented out the field taStockId in the class TaStockPrice. But still the same exception.

javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: ERROR: null value in column "tastockid" violates not-null constraint


The table in Postgresql looks like this :

CREATE TABLE tastockprice
(
id serial NOT NULL,
tastockid integer NOT NULL,
...
CONSTRAINT tastockprice_pkey PRIMARY KEY (id ),
CONSTRAINT tastockprice_tastockid_fkey FOREIGN KEY (tastockid)
REFERENCES tastock (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)

With my current setup, hibernate doesn't seem to set field tastockid. How Can I make sure that hibernate recognised tastockid as the foreign key, and sets this field when saving TaStock ?
Alexander Bondarev
Greenhorn

Joined: Jan 04, 2012
Posts: 12
Frank VanOor wrote:javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: ERROR: null value in column "tastockid" violates not-null constraint
...
With my current setup, hibernate doesn't seem to set field tastockid. How Can I make sure that hibernate recognised tastockid as the foreign key, and sets this field when saving TaStock ?

You have this exception because the column 'tastockid' is not included in SQL INSERT statement. The column definition has the element 'insertable = "false"'. Try to change it to 'insertable = "true"'.
Frank VanOor
Ranch Hand

Joined: Jun 18, 2004
Posts: 49
I change the colomn taStockId as follow

@Column(insertable = true)
private Integer tastockid;

Still the same exception :-(


The log of hibernate shows the following output:

Hibernate: insert into TaStock (active, buydate, buysell, selldate, slope, stockid, tapatternid, watch) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select currval('TaStock_id_seq')
Hibernate: insert into TaStockPrice (date, line, price, quality, tastockid) values (?, ?, ?, ?, ?)


It looks like hibernate doesn't use the Id of TaStock in the insert statement of TaStockPrice.


Alexander Bondarev
Greenhorn

Joined: Jan 04, 2012
Posts: 12
I meant to change like this:
Frank VanOor
Ranch Hand

Joined: Jun 18, 2004
Posts: 49
Hi Alexander , thank for help.
I changed like you suggested, but have the following exception now.


Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.stockdomain.domain.TaStockPrice column: tastockid (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:681)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:703)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:725)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:478)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1294)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1736)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
Alexander Bondarev
Greenhorn

Joined: Jan 04, 2012
Posts: 12
I am guessing this is because you are still having the field 'private Integer tastockid;' in the class TaStockPrice. Remove it from the class definition.
Frank VanOor
Ranch Hand

Joined: Jun 18, 2004
Posts: 49
I removed "private Integer tastockid" from TaStockPrice, and all goes fine !! Rows are inserted in DB.

thank you very much for you patience and help Alexander .

Frank
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Saving OneToMany , foreign key not set
 
Similar Threads
Hibernate 3 Annotations
java.sql.BatchUpdateException: ORA-02049
Heap exception while loading complete table data from database
@OneToMany primary key part of foreign key
AnnotationException: : wrong number of column