Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Saving OneToMany , foreign key not set

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to generate the entity ids somehow. For example:

 
Frank VanOor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant to change like this:
 
Frank VanOor
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic