1 Reply Latest reply on Aug 3, 2007 11:56 AM by waynebaylor

    Compound key in combination with the generatedValue

    mnouwens1

      Hello I am creating an entity user which hold an compound primairy key. consisting of id and department_id. The department_id is set by the code but the id should be generated by the Table Generator. BUt this value stay -1.

      Can any one point me where it could go wrong.

      At the moment i have:


      @Entity
      @IdClass(UserEntityPk.class)
      @Table(name = "KIEKEBOE_USERS")
      public class UserEntity implements java.io.Serializable {

      @TableGenerator(name = "userIdGen", table = "KIEKEBOE_SEQUENCE_GENERATOR", pkColumnName = "GEN_KEY", valueColumnName = "GEN_VALUE", pkColumnValue = "USER_ID", initialValue = 203, allocationSize = 1)
      @Id
      @GeneratedValue(strategy = GenerationType.TABLE, generator = "userIdGen")
      @Column(name = "USER_ID", nullable = false)
      private int id = -1;

      @Id
      @Column(name = "DEP_ID", nullable = false)
      private int depId = -1;


      public int getId() {
      return id;
      }
      public int getDepId() {
      return depId;
      }

      The Pk class is:

      @Embeddable
      public class UserEntityPk implements Serializable {


      private int id = -1;

      private int depId = -1;

      public UserEntityPk() {
      super();
      }

      @Id
      @Column(name = "DEP_ID", nullable = false)
      public int getDepId() {
      return depId;
      }

      @TableGenerator(name = "userIdGen", table = "KIEKEBOE_SEQUENCE_GENERATOR", pkColumnName = "GEN_KEY", valueColumnName = "GEN_VALUE", pkColumnValue = "USER_ID", initialValue = 203, allocationSize = 1)
      @Id
      @GeneratedValue(strategy = GenerationType.TABLE, generator = "userIdGen")
      @Column(name = "USER_ID", nullable = false)
      public int getId() {
      return id;
      }

      public void setDepId(int newDepId) {
      depId = newDepId;
      }

      public void setId(int newId) {
      id = newId;
      }

      }