Link to home
Start Free TrialLog in
Avatar of Micheal_Male
Micheal_MaleFlag for Afghanistan

asked on

hibernate save

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

Avatar of for_yan
for_yan
Flag of United States of America image


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.

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

Avatar of Micheal_Male

ASKER

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,
Avatar of Mick Barry
what are the definitions for the table keys?
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.
And how are your ids annotated?
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.
I don't think hibernate sets that for you, you need to handle it yourself
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.
Whats the id for Price, and how is it annotated
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
Having that (@ManyToOne) requires that you have set the other side of the relationship
Yes it was set. It was insertable=false and updateable = false which was causing the issue
Removing that fixed the issue.
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'.
> 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);
Yes I was.
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
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.
ASKER CERTIFIED SOLUTION
Avatar of josephtsang
josephtsang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes i was doing that and still it was the same issue
accepted answer says the same as I already stated earlier :)
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.