Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA persistance issue with hibernate

 
Raja Nagendra Kumar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class as below..

@Entity
@Table(name = "TBL_TIME_TEZZDS")
@AttributeOverrides({@AttributeOverride(name = "pk",
column = @Column(name = "pk_time_tezzd"))})
public final class TimeTezzD extends AbstractTezzD
{
@OneToMany(fetch = FetchType.EAGER,
cascade = CascadeType.ALL)
@JoinTable(name = "tbl_time_tezzds_map",
joinColumns = {@JoinColumn(name = "fk_time_tezzds",
nullable = false)},
inverseJoinColumns = {@JoinColumn(name = "fk_ref_abs_tezzds",
nullable = false)})
private final Map<DateTimeSpec, AbstractTezzD> timeTezzDMap = new HashMap<DateTimeSpec, AbstractTezzD>();

When a new TimeTezzD is created and new objects are created to timeTezzDMap with key and value also beeing new, on hibernate save of TimeTezzD object is resulting in

[java] 2) testValid(com.tejasoft.dialer.model.bdo.tns.time.test.TestTimeTez
zD)org.hibernate.TransientObjectException: object references an unsaved transien
t instance - save the transient instance before flushing: com.tejasoft.dialer.model.bdo.tns.time.DateTimeSpec

However every thing works fine, if I add already saved DateTimeSpec objects to the map timeTezzDMap.

Looking at ORM approach, I would expect saving TimeTezzD should automatically store entire map, even if the map key and values all are new.

Any reasons for such behaviour.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Looking at ORM approach, I would expect saving TimeTezzD should automatically store entire map, even if the map key and values all are new."

Let's look at it in database perspective, because you have that key, and based on your other post, I am assuming Hibernate is generating the schema. The schema must have a fk relationship to the Map Key elements. But it can't put in the id of the DateTimeSpec because those keys are transient which means they have no id, it is null. So if you try to put null in a FK field, you are going to get a Constraint violation.

2) I highly recommend not making that MAP eager loaded. Do you expect every single use case with the TimeTezzD class to have to have that data populated. Or are there times where you won't even need to go into the Map. In cases where you won't go into the Map, then you are bringing down data that you don't need, and slowing your system down.

Mark
 
Raja Nagendra Kumar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Let's look at it in database perspective, because you have that key, and based on your other post, I am assuming Hibernate is generating the schema. The schema must have a fk relationship to the Map Key elements. But it can't put in the id of the DateTimeSpec because those keys are transient which means they have no id, it is null. So if you try to put null in a FK field, you are going to get a Constraint violation.

Mark, at this time, what stops ORM tool to save the DateTimeSpec and get the valid fk..

Such thing is happening only for MAP and not for relationships such as

A containting B and if A Saved, B also gets saved in case B also is new, I would see the same logic is holding good in Map kind of storage too.

I appricate your time on this.

Regards,
Nagendra
 
Uzbek Jon
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm experiencing the same problem right now.

Did you find solution?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic