具有引用类的id的JPA实体

时间:2013-07-08 09:46:39

标签: jpa

假设我有一个ClassA类和一个ClassB类。 ClassA维护对具有OneToManny依赖关系的List的引用,并且具有应用程序设置的id。 ClassB没有引用ClassA。依赖是单向的。

由于每个JPA实体都需要一个Id,我必须为ClassB指定一个Id,但ClassB的id由ClassA的Id和ClassB的Id组成。有没有办法对此进行建模而不添加ClassB到ClassA的引用(我知道在这种情况下我可以使用@ManyToOne和@JoinColumn)?

修改

我正在尝试使用@Embeddable@ElementCollection,但我收到AnnotationException: List/array has to be annotated with an @OrderColumn (or @IndexColumn): my.package.PoiDetails.tips

以下是完整代码(PoiDetailsClassACommentClassB)。您可以假设Comment主键是User的ID,Comment的时间戳和Poi的ID的组合。

ClassA的:

@Entity
public class PoiDetails implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@OneToOne
@JoinColumns(value = { 
        @JoinColumn(name="poiId", referencedColumnName="id"),
        @JoinColumn(name="categoryId", referencedColumnName="categoryId"),
        @JoinColumn(name="providerId", referencedColumnName="providerId")})
private Poi poi;

@Transient
private User[] peopleHere;


@ElementCollection
  @CollectionTable(
        name="tips"
  )  
private Comment[] tips;

@ElementCollection
  @CollectionTable(
        name="queries"
  )
private Comment[] queries;

}

ClassB的:

@Embeddable
public class Comment implements Serializable {

private static final long serialVersionUID = 1L;
private String text;
@OneToOne
@JoinColumn(name="userId")
private User user;
private long timestamp;

public Comment(String text, User user, long timestamp) {
    super();
    this.text = text;
    this.user = user;
    this.timestamp = timestamp;
}
public Comment(){}

public String getText() {
    return text;
}
public void setText(String text) {
    this.text = text;
}

public long getTimestamp() {
    return timestamp;
}

public void setTimestamp(long timestamp) {
    this.timestamp = timestamp;
}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}
}

POI:

@Entity
public class Poi implements Serializable {

private static final long serialVersionUID = 1L;

@Id
private long id;
@Id
private long categoryId;
@Id
private long providerId;

private String title;

private String description;

private String positionLat;

private String positionLong;

private String positionAlt;

public Poi(){}

public Poi(long id, String title, String description, String positionLat, String positionLong, String positionAlt) {
    this(id, -1, title, description, positionLat, positionLong, positionAlt);
}

public Poi(long id, long categoryId, String title, String description, String positionLat, String positionLong, String positionAlt) {
    this.id = id;
    this.categoryId=categoryId;
    this.title = title;
    this.description = description;
    this.positionLat = positionLat;
    this.positionLong = positionLong;
    this.positionAlt = positionAlt;
}

public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public String getPositionLat() {
    return positionLat;
}
public void setPositionLat(String positionLat) {
    this.positionLat = positionLat;
}
public String getPositionLong() {
    return positionLong;
}
public void setPositionLong(String positionLong) {
    this.positionLong = positionLong;
}
public String getPositionAlt() {
    return positionAlt;
}
public void setPositionAlt(String positionAlt) {
    this.positionAlt = positionAlt;
}

public long getCategoryId() {
    return categoryId;
}

public void setCategoryId(long categoryId) {
    this.categoryId = categoryId;
}

public long getProviderId() {
    return providerId;
}

public void setProviderId(long providerId) {
    this.providerId = providerId;
}
}

用户:

@Entity
public class User implements Serializable {

private static final long serialVersionUID = 1L;
@Id
private String id;
private String nickname;
private String thumbnailUrl;

public User(){}

public User(String userId, String nickname){
    this(userId, nickname, null);
}

public User(String id, String nickname, String thumbnailUrl) {
    this.id=id;
    this.nickname=nickname;
    this.thumbnailUrl=thumbnailUrl;
}

public User(String userId) {
    this.id=userId;
}
public String getId() {
    return id;
}
public void setId(String userId) {
    this.id = userId;
}

public void setNickname(String nickname) {
    this.nickname=nickname;
}

public String getNickname() {
    return nickname;
}

public String getThumbnailUrl() {
    return thumbnailUrl;
}

public void setThumbnailUrl(String thumbnail) {
    this.thumbnailUrl = thumbnail;
}

}

1 个答案:

答案 0 :(得分:1)

添加ManyToOne是最佳解决方案。

您还可以为外键字段添加Basic,但这不是一个好的设计。

另一种方法是将ClassB映射为Embeddable and use和ElementCollection映射,

请参阅, http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection