Results 1 to 9 of 9
  1. #1
    jhasell is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default Hibernate adding/overwriting records incorrectly

    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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    I suspect this is all about how you are working with these objects.
    Taking question 1, how do you add a Schedule? Specifically how do you add the existing Agreement to the Schedule?

  3. #3
    jhasell is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    The Agreement is being added to the schedule using the Schedule setter function and then the Schedule object is written to the database using javax.persistence.EntityManager.persist.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Are you sure it's the existing Agreement?
    And simply describing the situation really isn't the best way of handling this.
    Some code would make life a lot easier.

  5. #5
    jhasell is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    The Agreement is added on a separate page, and then when the Schedule is added, the name of all Agreements in the database are listed in a drop down box and the user chooses one. The code adding them is:

    @Override
    public String add(final R resource) {
    final Query query = entityManager.createQuery("from Resource where id = :id");

    String id;
    Resource existing;

    query.setMaxResults(1);

    do {
    id = identifier.identify(resource);
    existing = get(entityManager, Resource.class, id);
    } while (existing != null && identifier.collision(id, existing, resource));

    if (existing == null) {
    resource.setId(id);
    entityManager.getTransaction().begin();
    onBeforePersist(id, resource);
    entityManager.persist(resource);
    onAfterPersist(id, resource);
    entityManager.getTransaction().commit();

    return id;
    }

    return null;
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    So when a Schedule is created there is no Agreement?
    That works OK?

    I can make neither head nor tail of that above code...it's far too generic compared to the problem you are describing.

  7. #7
    jhasell is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    It is inherited code made using Google Guice. I have trouble making sense of it too,

    However, before I added the AgreementPropety class, the code worked, although not very efficiently. Which is why I believe it has something to do with how the classes are declared.

  8. #8
    jhasell is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    BTW, the Schedule is added to the database, but the Agreement is added a second time and the AgreementProperty entires are overwritten with the new Agreement key.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    I fear I would be as lost as you then.
    Never used Guice, so I can't say one way or the other.

Similar Threads

  1. Array overwriting data
    By hobo in forum New To Java
    Replies: 7
    Last Post: 10-27-2010, 01:29 PM
  2. Throttling records
    By jitman in forum New To Java
    Replies: 0
    Last Post: 03-11-2010, 06:18 PM
  3. Replies: 0
    Last Post: 03-05-2010, 07:11 PM
  4. insert records
    By rudravaram in forum JDBC
    Replies: 3
    Last Post: 01-03-2010, 05:54 PM
  5. Replies: 2
    Last Post: 06-30-2007, 03:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •