0

假设我有一个 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.

这是完整的代码(取PoiDetailsasClassACommentas ClassB)。您可以假设Comment主键是User' id、Comment' 时间戳和Poi' id 的组合。

A类:

@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;

}

B类:

@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;
}
}

宝:

@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;
}

}
4

1 回答 1

1

添加 ManyToOne 是最好的解决方案。

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

另一种选择是将 ClassB 映射为 Embeddable 并使用和 ElementCollection 映射,

见, http ://en.wikibooks.org/wiki/Java_Persistence/ElementCollection

于 2013-07-08T13:25:32.227 回答