-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate adding/overwriting records incorrectly
PostPosted: Mon Jul 04, 2011 1:12 pm 
Newbie

Joined: Mon Jul 04, 2011 12:57 pm
Posts: 1
I am a HIbernbate newbie and have a problem that has been bothering me for days. Here is a rundown of the issue I am having:

1. There is a @Entity class called Resource which contains a Key field and an ID field.
2. There is a @Entity class called Agreement which extends Resource.
3. There is a @Entity class called Schedule which extends Resource and contains an Agreement field.
4. There is a @Entity class called AgreementProperty.
5. There is a @Entity class called PositionAgreementProperty which extends AgreementProperty.
6. There is a @Entity class called ShiftAgreementProperty which extends AgreementProperty.
7. Agreement has a field which is a List of PositionAgreementProperty classes.
8. Agreement has a field which is a List of ShiftAgreementProperty classes.
9. When I add an Agreement via the web page, Hibernate populates:
a. the Agreement table with one record that contains nothing but an agreement_key of 1
b. the Resource table with one record, including the agreement_key and a generated ID
c. the AgreementProperty table with 5 records containing keys 1 - 5.
d. the PositionAgreementProperty table that contains the agreement_key (always set to 1) and the 1, 2, 3 AgreementProperty keys
e. the ShiftAgreementProperty table that contains the agreement_key (always set to 1) and the 4, 5 AgreementProperty keys
10. When I then add a Schedule via the web page, which includes the name of the Agreement from the database, Hibernate populates:
a. the Agreement table with new record with an agreement_key of 2.
b. the Resource table with a new record with an agreement_key of 2 and a generated ID of NULL
c. the Resource table with a second new record for the Schedule.
11. The PositionAgreementProperty and ShiftAgreementProperty tables have their agreement_key fields updated from 1 to 2, for the "new" Agreement.
12. Now Agreement 1 does not have any PositionAgreementProperty nor ShiftAgreementProperty records associated with it.

What I am trying to figure out is:
1. Can I prevent a new Agreement from being added and just use the previous agreement_key
a. I think Hibernate does this because both Agreement and Schedule get their key values by inheriting from Resource
2. If not, can I make Hibernate insert new AgreementProperty records instead of updating the existing ones?

The following are the class definitions (with the getters and setters removed for the sake of brevity):

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@XmlRootElement
public class Resource implements Serializable {
@Id
@Column(name = "_key")
@GeneratedValue
@XmlTransient
private Long key;

@Column(unique = true)
@XmlTransient
private String id;
}

@Entity
@XmlRootElement
public class Agreement extends Resource {
@JoinColumn(name = "agreement__key", referencedColumnName = "_key")
@OneToMany(cascade = CascadeType.ALL)
private List<PositionAgreementProperty> positionProperties = new ArrayList<PositionAgreementProperty>();

@JoinColumn(name = "agreement__key", referencedColumnName = "_key")
@OneToMany(cascade = CascadeType.ALL)
private List<ShiftAgreementProperty> shiftProperties = new ArrayList<ShiftAgreementProperty>();
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@XmlRootElement
public class AgreementProperty implements Serializable {
@Id
@Column(name = "_key")
@GeneratedValue
@XmlTransient
private Long key;
}

@Entity
@XmlRootElement
public class PositionAgreementProperty extends AgreementProperty {
public PositionAgreementProperty() {
super();
}
}

@Entity
@XmlRootElement
public class ShiftAgreementProperty extends AgreementProperty {
public ShiftAgreementProperty() {
super();
}
}

@Entity
@XmlRootElement
public class Rotation extends Resource {
}

@Entity
@XmlRootElement
public class Schedule extends Rotation {
@OneToOne(cascade = CascadeType.ALL)
private Agreement agreement;
}


Any help offered would be appreciated. I am not sure what kind of code examples I should include besides the class declarations since the code doing the "work" was generated via Google Guice. Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.