0

我的实体类有共同的属性。他们必须有Id - 序列longDate createdDate显示何时将实体的元组添加到 DB 和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>

所以我制作了具有关键属性的公共类:

@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,数据库中的所有元组都以 0 作为 id。这是什么意思。有任何想法吗?

4

1 回答 1

1

Using inheritance maybe?

@MappedSuperclass
public abstract class AbstractArticle implements Serializable { /* ... */ }

@Entity
public class BBCNews extends AbstractArticle { /* ... */ }
于 2015-01-19T21:26:35.473 回答