我的Entity类有共同的属性。
他们必须有 Id - 序列long
号码
Date
createdDate ,显示实体的元组何时添加到数据库和Date
lastModifiedDate 属性,该属性显示何时完成最后一次修改一个元组。
我使用JPA 2.1。 ,Hibernate 4.3.6.Final
<depedencies>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
所以我用关键属性创建了公共类:
@Embeddable
public class AbstractArticle implements Serializable {
private static final long serialVersionUID = 895868474989692116L;
@Nonnull
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false)
private long id;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "createdDate", nullable = false)
private Date createdDate;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "lastModifiedDate", nullable = false)
private Date lastModifiedDate;
public AbstractArticle() {
}
... //getters and setters
}
和另一个使用此公共密钥值的类
@Entity
public class BBCNews{
@Nonnull
@Column(name = "newsId", nullable = false)
private long newsId;
@Nonnull
@Column(name = "title", nullable = false)
private String title;
@CheckForNull
@Column(name = "description", nullable = true)
private String description;
@Id
private AbstractArticle abstractArticle;
public BBCNews() {
}
...//getters and setters
确定一切正常,但长id AbstractArticle是相同的0,DB中的所有元组都是0作为id。这个的目的是什么呢?任何想法?
答案 0 :(得分:1)
使用继承可能吗?
@MappedSuperclass
public abstract class AbstractArticle implements Serializable { /* ... */ }
@Entity
public class BBCNews extends AbstractArticle { /* ... */ }