Problem persisting in SPRING-DATA/JPA using Composite Key / Does not return Generated Id

Asked

Viewed 95 times

1

Hello someone already had this problem, when saving a class with composite key, the Spring Data API does not return with the generated id?

I ran a test with primitive classes with unique id so it works normally and returns the generated id.

Follows the classes

@Entity
@Table(name = "SMSEnviado")
public class SMSEnviado implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected SMSEnviadoPK sMSEnviadoPK;
    @Basic(optional = false)
    @Column(name = "mensagem")
    private String mensagem;
    @Basic(optional = false)
    @Column(name = "dataEnvio")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dataEnvio;  

    // gets and sets()...


}
@Embeddable
public class SMSEnviadoPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "idSMSEnviado")
    private int idSMSEnviado;
    @Basic(optional = false)
    @Column(name = "semana")
    private short semana;
    @Basic(optional = false)
    @Column(name = "ano")
    private short ano;
    @Basic(optional = false)
    @Column(name = "codigoRevenda")
    private String codigoRevenda;

    // gets and sets()...

}

idSMSEnviado = null

Use the SMSEnviadoPK thus:

SMSEnviadoPK smsEnvioPk = new SMSEnviadoPK();
smsEnvioPk.setAno((short)ano);
smsEnvioPk.setCodigoRevenda(revenda.getCodigorevenda());
smsEnvioPk.setSemana((short)semana);
SMSEnviado smsEnviado = new SMSEnviado(smsEnvioPk);
smsEnviado.setDataEnvio(new Date());
smsEnviado.setMensagem(sms.getMensagem());
smsEnviado = enviadoImpl.save(smsEnviado);
  • Where and how you call the setters SMSEnviadoPK?

  • Solved.. because you had to close the JPA commit.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.