我有以下两个类(及其pk类)和这些注释。我删除了setter / getters / hashcode / equals以压缩示例
我最终收到此错误
org.hibernate.MappingException: Repeated column in mapping for collection: com.stackOverflow.Features.thingCategory column: YEAR
我的猜测是因为“年”在连接表中共享,我搞砸了一些用于关联我的实体的语法。请注意,这些视图在其实体之间具有隐式关系,我试图在我的注释中建模。我尝试了一些JPA建模工具,它们在建模后给出了相同的错误。我已尝试将连接列设置为insertable
,updatable
设置为false
。
我当然可以写一个SQL查询,但我真的很惊讶Hibernate在我看来有多难。
功能实体
@Entity
@Table(name = "myTableOfFeatures")
public class Feature {
private static final long serialVersionUID = 1L;
@EmbeddedId
private FeatureKey id;
@Column(name="displayText")
private String description;
//bi-directional many-to-many association
@ManyToMany
@JoinTable(
name="joinTableToThingCategories"
, joinColumns={
@JoinColumn(name="name", referencedColumnName="name", insertable = false, updatable = false),
@JoinColumn(name="year", referencedColumnName="year", insertable = false, updatable = false)
}
, inverseJoinColumns={
@JoinColumn(name="year", referencedColumnName="year", insertable = false, updatable = false),
@JoinColumn(name="title", referencedColumnName="title", insertable = false, updatable = false)
}
)
private List<ThingCategory> thingCategory;
public Feature() {
}
// ... gets and sets
}
@Embeddable
public class FeatureKey implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="name")
private String myName;
private String year;
public FeatureKey() {
}
// ... gets and sets and equals and hashes
}
ThingCategory实体
@Entity
@Table(name = "CategoriesOfThings")
public class ThingCategory implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ThingCategoryKey id;
private String comment;
//bi-directional many-to-many association to categories
@ManyToMany(mappedBy="thingCategory")
private List<Feature> features;
public ThingCategory() {
}
// ... gets and sets
}
@Embeddable
public class ThingCategoryKey implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
private String year;
@Column(name="categoryName")
private String title;
public ThingCategoryKey() {
}
// ... gets and sets and equals and hashes
}
答案 0 :(得分:0)
这是JPA 2.1规范中的一个例子。 我希望它有所帮助。
@Entity
public class Employee {
@Id int id;
@Embedded ContactInfo contactInfo;
...
}
@Embeddable
public class ContactInfo {
@ManyToOne Address address; // Unidirectional
@ManyToMany List<PhoneNumber> phoneNumbers; // Bidirectional
}
@Entity
public class PhoneNumber {
@Id int phNumber;
@ManyToMany(mappedBy="contactInfo.phoneNumbers")
Collection<Employee> employees;
}
我强烈推荐你这个规格。 JPA 2.1
答案 1 :(得分:0)
这可能有用,但我没有尝试过;所以我想把它写成评论,但把代码写成评论很麻烦。所以你可能想尝试它,如果它工作(抱歉没有时间尝试)。如果它不起作用,请给我留言,我会删除它。
@JoinTable(
name="joinTableToThingCategories",
joinColumns={
@JoinColumn(name="feature_name", referencedColumnName="name", insertable = false, updatable = false),
@JoinColumn(name="feature_year", referencedColumnName="year", insertable = false, updatable = false)
},
inverseJoinColumns={
@JoinColumn(name="tcat_year", referencedColumnName="year", insertable = false, updatable = false),
@JoinColumn(name="tcat_title", referencedColumnName="title", insertable = false, updatable = false)
}
)
private List<ThingCategory> thingCategory;