• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem Persisting Entity In Hibernate

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have been facing a very typical problem since the past 7-8 hours.
I have been trying to persist an entity instance using hibernate. The entity maps to a database table whose columns have a 'not null' constraint.

I initialize all the instance variables of the entity before calling the persist method. I have a @PrePersist, and @PostPersist method that prints the values of these instance variables. However, when hibernate generates sql for the database, it is inserting null values in the columns and hence failing with constraint violation errors.

The column mappings are correct, or else even the insert statements would not have been generated by hibernate. I am going absolutely bald over the issue and have not even the slightest idea as to why am i getting a null while even the prePersist method prints the initialized values.

Has anyone faced a similar problem? Any solution??

Regards,
Ryan Sukale
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you using container managed transation or application managed transation. If you are using container managed transation, then you need to open transation and then use persist method. after that donot forget to comit the transation and closting the sesion. After comitting only values will be reflected into database.

Please try above or send us code block what you are trying to persist.
 
ryan sukale
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi xsunil,

here is the code

I feel like i have hit a wall. Its been 3 days since i have been unable to make any progress.

All the properties of the objects are populated before the method is called. The 'MyEntity' already exists in the database, so merge does not fail.

The code is to insert a option which is of type 'SubEntity' into the database. If i remove the not null constraints from the database, it inserts the rows successfully, but except the id's, all the remaining columns get a null or their default values.

What am i doing wrong? Even the prepersist method in the entity SubEntity and BaseEntity print the values of their instances to the values that i have initialized. But its just not going into the database.



Regards,
Ryan Sukale
 
xsunil kumar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you send me your entity mapping. After looking on entity mapping only we can come to root cause.
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mentioned SubEntity and BaseEntity. Does this means you are using inheritance.. If yes, then out of 3, which inheritance strategy you are using??
It looks like something is wrong with the inheritance ORM mapping...

Please post your inheritance ORM mapping..
 
ryan sukale
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the joined inheritance strategy.

I dont know why, but hibernate is not populating the discriminator column in the table. I need to access the discriminator column value in elsewhere, so i had to create it as a property and populate it using an enum.

Here is the base entity


Here is the subclass entity.






Before passing objects of this class to the addOption method, i ensure that all its property setters are called so that the instance variables are properly initialized. And i print the values of all the instance variables in the prepersist method, for both, the base class and the sub class(the other properties i have not shown here, which but i have also set. I know they are set bcos of the log of the prepersist displays them).

Something very small is wrong with the whole thing, and its just managing to escape my notice.. Its frustrating. I am using the same entities elsewhere and they are perisiting just fine, so i know that the inheritance and class mappings are working perfectly, except for the fact that hibernate is not helping me to fill in the discriminator type column, and neither is that of my concern anymore. Its only when i call the addOption method that the commit fails.

Kindly help.

Regards,
Ryan Sukale
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have written discriminator column to be type INTEGER and in subclass you are passing "0" (String)... This seems incorrect..
 
ryan sukale
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep,

Its appropriate. I have been referencing Pro EJB 3 Java Persistence API while writing the code, and there is a clear example of using the discriminatorcolumn in the same way that i have done here.

Moreover, hibernate does not need to use the discriminator column, unlike other implementations. Thats what i have found out after googling around a bit.

Had it been an issue, i would never have been able to perisist these entities elsewhere, or when i remove the not null constraint on my database column.

Anyways, Thanks for trying. But I still cant find out whats wrong...

Regards,
Ryan Sukale
reply
    Bookmark Topic Watch Topic
  • New Topic