Search code examples
hibernatejpakeycomposite

Hibernate/JPA - Is possible to use @Id and @EmbededId at same time?


i´m a little confused to add a business key in my entity mapping. All entities uses Long as a Id, but now i have to create a composite Id, my doubt is, can i mixed @Id and @EmbeddedId together or only the embedded object must be the Id alone?

Here is the code:

@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractEntity implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 6891295574206401221L;

@Id
@GenericGenerator(
        name = "seq_id",
        strategy = "br.com.alianca.customerservicemacros.entity.AliancaSequenceGenerator")
@GeneratedValue(generator = "seq_id")
private Long id;

@Column(name = "dt_created")
private Date created;

@Column(name = "dt_altered")
private Date altered;

the embeddable class:

@Embeddable
public class DacsInfo implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 5960251258518073347L;

/**
 * Código Docsys do Navio
 */
@Column(name = "COD_VESSEL", length = 4)
private String vessel;

/**
 * Código do navio.
 */
@Column(name = "NUM_VOYAGE", length = 5)
private String voyage;

/**
 * Primeiros 4 digitos do código do <code>BLUI</code>.
 */
@Column(name = "PREFIX_BLUI", length = 4)
private String prefixBlui;

/**
 * Número universal do BL.
 */
@Column(name = "NUM_BLUI", length = 12)
private String blui;

and here the final entity:

@Entity
@Table(name = "FATO_DACS_REPT", schema = "u_cs_service")
public class FatoDacsRept extends AbstractEntity {

/**
 * 
 */
private static final long serialVersionUID = 9148311315020469420L;

@EmbeddedId
private DacsInfo dacsInfo;

@Column(name = "DAT_INPUT")
private Date loading;

/**
 * Código da companhia (Hamburg Sud - 699 / Aliança - 690)
 */
@Column(name = "COD_COMPANY", length = 4)
private String company;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "fatoDacsRept")
private List<FatoDacsReptChq> pendencias;

Solution

  • I think not...

    JPA 2.0 final spec page 373:

    11.1.15 EmbeddedId Annotation

    ...

    There must be only one EmbeddedId annotation and no Id annotation when the EmbeddedId annotation is used.

    ...