1 Reply Latest reply on Jun 21, 2012 1:04 PM by Ben James

    Autogenerated Field in EmbeddedId

    Osvaldo González Newbie

      I have a table with a composite Id of two fields, one autogenerated and one Foreign Key. In the Entity class I have the following Code:

       

      @Entity
      @Table(name = "personascambios")
      public class PersonasCambios implements Serializable {
          private static final long serialVersionUID = 1L;
          @EmbeddedId
          protected PersonasCambiosPK personasCambiosPK;
          @Basic(optional = false)
          @Column(name = "activo")
          private short activo;
          @Basic(optional = false)
          @Column(name = "dt_afecta")
          @Temporal(TemporalType.TIMESTAMP)
          private Date dtAfecta;
          @Basic(optional = false)
          @Column(name = "usu_afecta")
          private String usuAfecta;
          @JoinColumn(name = "idPersona", referencedColumnName = "idPersona", insertable = false, updatable = false)
          @ManyToOne(optional = false)
          private Personas personas;
      
      

       

      And in the Id Class I have the following code:

       

      @Embeddable
      public class PersonasCambiosPK implements Serializable {
          @Basic(optional = false)
          @Column(name = "idPersonaCambio")
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          private long idPersonaCambio;
          @Basic(optional = false)
          @Column(name = "idPersona")
          private long idPersona;
      

       

      It's that the right way to declare the autogenerated field? How should I persist the PersonasCambios entity? I create a new PersonasCambiosPK object, and set the idPersonaCambio to 0L, and the idPersona to an id obtained from de DB, it works the first time, but when I want to persist a new PersonasCambios using the same idPersona, the application throws an Entity Exists Exception.

       

      Thanks for your help in advance.