Solved

hibernate save

Posted on 2011-02-20
22
793 Views
Last Modified: 2012-05-11
I have A class which extends B class. Below is a similar entity classes ihave. Now when i do this :-

A a = new A();
a.setName("name");
hibernateDao.createObject(a);

Prices prices = new Prices():
prices.setX("");
hibernateDao.saveObject(prices);
a.getPrices().add(prices);

I get an exception because when it tries to insert in prices table it states (id) is null. I looked into the logs and saw the insert statement which hibernate tries to insert and it has all the columns needed to insert for prices but does not have (id) column which is a foreign key to B class. What am i doing wrong ?>


@Table(name="productA")
public class A extends B {
private Set<Prices> prices = new HashSet<Prices>();
//setter
@OneToMany(fetch=FetchType.LAZY, mappedBy="a")
public Set<Prices> getPrices() {
return prices
}
}

@Entity
@Table(name="productB")
public class B implements Serializable {
private String id;
.
.
public String getId() {
//generate Id
@Column(name="id)
public String getId() {
}
}

@Entity
@Table(name="prices")
public class Prices {
private A a;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id")
public A getA() {
return a;
}

Open in new window

0
Comment
Question by:Micheal_Male
22 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34939904

Isn't it something similar to what we find in this description from

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html
(see below).
I cannot fully understand it myself, but words in this example and in discussion around it
seems to be very much related to your problem:
---------------------------------
You can define the foreign key name generated by Hibernate for subclass tables in the JOINED inheritance strategy.

@Entity

@Inheritance(strategy = InheritanceType.JOINED)

public abstract class File { ... }


@Entity

@ForeignKey(name = "FK_DOCU_FILE")

public class Document extends File {

The foreign key from the Document table to the File table will be named FK_DOCU_FILE.

-------------------------------------------------------

0
 

Author Comment

by:Micheal_Male
ID: 34939930
only if it did work. I saw tons of people having issues when using Inheritance. You really don't need to define an abstract class. Also Ineheritance Strategy is already defined in the super class. What i am trying to figure out is when i persist the new Prices object i get an error that id is a not null column which is true because it's a foreign key. However the log states that when persisting the Prices object the insert statement looks like :-

insert into com.Prices (x,y,z) values (null,null,1) and i get an exception where in reality it should be :-
insert into com.Prices (x,y,z,id) values (....).

Now if i don't insert Prices then all is well. The data in both tables A and B are inserted with appropriate values inserted. Don't know why it is doing that,
0
 
LVL 92

Expert Comment

by:objects
ID: 34939960
what are the definitions for the table keys?
0
 

Author Comment

by:Micheal_Male
ID: 34939974
I don't have it written cause it's in the data model definition. However, there is a foreign key id in Prices table to Class B table.
0
 
LVL 92

Expert Comment

by:objects
ID: 34940036
And how are your ids annotated?
0
 

Author Comment

by:Micheal_Male
ID: 34940059
It's using the hibernate GenericGenerator Strategy. Ids are working fine for other Entities also.
A and B both share the same primary key column which is id and Prices table have a foreign key reelation on id in table productB.
0
 
LVL 92

Expert Comment

by:objects
ID: 34940568
I don't think hibernate sets that for you, you need to handle it yourself
0
 

Author Comment

by:Micheal_Male
ID: 34940583
well i am doing this :-

hibernateDao.saveObject(prices).

I get a Data Integrity Vilolation that id is null. And the log is not outputting the id in in the insert statement. I don't know whats really happening. I have set the values and everything but yet it still complains.
0
 
LVL 92

Expert Comment

by:objects
ID: 34940653
Whats the id for Price, and how is it annotated
0
 

Author Comment

by:Micheal_Male
ID: 34940773
It was the annotation used on the getter of Price to A class with @ManyToOne(insertable =false, updatable=false). removing it did solve the problem but wonder what was the reason for that annotation though. Myself a little new and tryng to learn hibernate
0
 
LVL 92

Expert Comment

by:objects
ID: 34940865
Having that (@ManyToOne) requires that you have set the other side of the relationship
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Micheal_Male
ID: 34940872
Yes it was set. It was insertable=false and updateable = false which was causing the issue
0
 

Author Comment

by:Micheal_Male
ID: 34940876
Removing that fixed the issue.
0
 

Author Comment

by:Micheal_Male
ID: 34940902
They had insertable=false and updatable = false because of the composite primary keys. However, i just can't find any other way to persist the data without removing those annotation. I tried hibernateDao.mergeObject and it states that 'a transient instance must be saved before persisting'.
0
 
LVL 92

Expert Comment

by:objects
ID: 34940998
> Yes it was set. It was insertable=false and updateable = false which was causing the issue

No, you weren't setting the value of the parent A

prices.setA(a);
0
 

Author Comment

by:Micheal_Male
ID: 34941011
Yes I was.
0
 
LVL 92

Expert Comment

by:objects
ID: 34941074
not that I could see in the code you posted
Its hard to work out what your problem is if you only post part of your code
0
 

Author Comment

by:Micheal_Male
ID: 34944535
Right on objects. I've been working in hibernate for a while and I know you had to set the value to onetomany association in prices which I did.but still it was not adding the id column when inserting which was fixed by removing inserrable = false. I was also saving the A object before persisting on prices.
0
 
LVL 5

Accepted Solution

by:
josephtsang earned 500 total points
ID: 35165370
So are you having something like this:

A a = new A();
a.setName("name");
hibernateDao.createObject(a);

Prices prices = new Prices():
prices.setX("");
prices.setA(a); <-- setting the parent to price
hibernateDao.saveObject(prices);

a.getPrices().add(prices);
...

Did you set the "parent" A into price before saving price?

Just for a bit manageable the parent-child relationship should be maintained on one side only, preferrably by Price. Did you set inverse="true" on A at the method getPrices() ?

It would be better if the setting of A into Price and the adding of prices into A's price lists are placed together into a method by A, to guarantee the consistency of the relationship on both sides.
0
 

Author Closing Comment

by:Micheal_Male
ID: 35310697
Yes i was doing that and still it was the same issue
0
 
LVL 92

Expert Comment

by:objects
ID: 35310714
accepted answer says the same as I already stated earlier :)
0
 

Author Comment

by:Micheal_Male
ID: 35310726
Objects. Sorry about that. I wanted to delete this question because i still did not use any of the solutions provided because i was still getting the same error. I just wanted to close the question as i was not able to ask a question because of this open question ;--(. Sorry about that or if there is a way i can revert it back i will definitely do that.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now