[英]How to generate id for diferent child class entities using jpa and hibernate
[英]How to separate key common class for entities using JPA and Hibernate?
我的Entity类具有共同的属性。 他们必须有ID -序列long
数Date
createdDate当实体的元组被添加到数据库并显示Date
lastModifiedDate属性,它表示在做一个元组的最后一次修改。
我使用JPA 2.1。 ,Hibernate 4.3.6。最终版
<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的ID相同,为0,数据库中的所有元组的id均为0。 这是什么目的。 有任何想法吗?
也许使用继承?
@MappedSuperclass
public abstract class AbstractArticle implements Serializable { /* ... */ }
@Entity
public class BBCNews extends AbstractArticle { /* ... */ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.