如何为使用 JPA 和 Hibernate 的实体分离密钥公共 class?
How to separate key common class for entities using JPA and Hibernate?
我的实体 classes 有共同的属性。
他们必须有 Id - 序列号 long
Date
createdDate 显示何时将实体的元组添加到数据库和 Date
lastModifiedDate 属性 显示最后一次修改元组的时间。
我使用 JPA 2.1。 ,休眠 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>
所以我用关键属性制作了通用 class:
@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
}
和另一个 class 使用此公共键值
@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
好的,一切正常,但是 long id AbstractArticle 是相同的 0,数据库中的所有元组的 id 都是 0。这是什么目的。有什么想法吗?
可以使用继承吗?
@MappedSuperclass
public abstract class AbstractArticle implements Serializable { /* ... */ }
@Entity
public class BBCNews extends AbstractArticle { /* ... */ }
我的实体 classes 有共同的属性。
他们必须有 Id - 序列号 long
Date
createdDate 显示何时将实体的元组添加到数据库和 Date
lastModifiedDate 属性 显示最后一次修改元组的时间。
我使用 JPA 2.1。 ,休眠 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>
所以我用关键属性制作了通用 class:
@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
}
和另一个 class 使用此公共键值
@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
好的,一切正常,但是 long id AbstractArticle 是相同的 0,数据库中的所有元组的 id 都是 0。这是什么目的。有什么想法吗?
可以使用继承吗?
@MappedSuperclass
public abstract class AbstractArticle implements Serializable { /* ... */ }
@Entity
public class BBCNews extends AbstractArticle { /* ... */ }