Announcement Announcement Module
Collapse
No announcement yet.
How to change primary key class value? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to change primary key class value?

    Hello,

    i am trying to change the value of one member variable that is part of a primary key class (posNr) after the value is initially set. The changed value isn't persisted. The old value is still in the database.

    I have the following bean:

    public class OrderPosition implements Serializable {

    private OrderPositionPK orderPositionPK;

    //other member variables and getter/setter
    }

    and the hbm.xml-definition for this bean:

    <class name="OrderPosition" table="Auftrag2" lazy="true">

    <composite-id name="orderPositionPK" class="OrderPositionPK">
    <key-property name="orderId" column="Nr" />
    <key-property name="year" column="Jahr" />
    <key-property name="posNr" column="Zeilennr" />
    </composite-id>
    </class>


    The primary key class looks like following:

    public class OrderPositionPK implements Serializable {

    private static final long serialVersionUID = -8916031215924912095L;
    private long orderId;
    private long posNr;
    private long year;
    }

    I am not able to change the posNr with:

    orderPosition.getOrderPositionPK().setPosNr(i);

    And i can instantiate and assign a new primary key class with:

    OrderPositionPK orderPositionPK = new OrderPositionPK();
    orderPositionPK.setOrderId(orderPosition2.getOrder PositionPK().getOrderId());
    orderPositionPK.setYear(orderPosition2.getOrderPos itionPK().getYear());
    orderPositionPK.setPosNr(i);
    orderPosition.setOrderPositionPK(orderPositionPK);

    The value in the database stays the same. No error is thrown. What could be the reason?

  • #2
    Please use [ code][/code ] tags when posting code that way it remains readable.

    You cannot change a primary key of a class once it has been set. Basically primary keys are immutable.

    Comment


    • #3
      This is weird. From the database side there is no restriction to change the composite key. Do i have generate a new bean with the intended primary key, then copy the values and after that delete the old bean?

      Comment


      • #4
        Think about it... You have something that uniquely identifies an object/record and afterwards you change that. If something would have a dependency (foreign key) to that record it would break.

        Comment


        • #5
          Hello Marten,

          thank you for your answer. If i could change the underlying database i would do it. But as with most projects there is a legacy system to adapt. One part of the composite key is the position number of the order position table. The legacy system generates the invoices based on the order and order position data that i fill. If the user of the program deletes an order position then there is a gap between the position numbers that must not be. I have to change all subsequent position numbers. Therefore it would be great if the primary key class could change.

          It seems that i have to generate new order positions with the needed position numbers, copy the values and handle the foreign key issues.

          Comment

          Working...
          X