3

我有点困惑在我的实体映射中添加业务键。所有实体都使用 Long 作为 Id,但现在我必须创建一个复合 Id,我的疑问是,我可以将 @Id 和 @EmbeddedId 混合在一起还是只有嵌入的对象必须是单独的 Id?

这是代码:

@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;

可嵌入类:

@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;

这里是最终实体:

@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;
4

1 回答 1

2

我想不是...

JPA 2.0 最终规范第 373 页:

11.1.15 EmbeddedId注解

...

使用 EmbeddedId 注解时,EmbeddedId 注解必须只有一个且没有 Id 注解。

...

于 2013-01-15T23:42:44.547 回答