Hibernate / JPA 具有多对多关系的元素集合?
这是一个奇怪的场景,因为我正在使用一个我无法更改其结构的遗留系统。
场景是我有一个元素表,其中包含有关元素的各种数据以及重要字段“类型”和“值”。
对类型和值的各种组合进行分类。例如,类型 2 和值“绿色”的所有元素都属于类别 1。
我正在遗留系统中构建一个用户界面,让用户在给定类型和值(显然)的情况下定义这些类别。我想要的是遗留系统在其休眠实体中包含一组类别。 Category 实际上是 ElementCategory 类型的枚举,我的职责之一是遗留应用程序必须是定义我的架构的应用程序,因此当我启动它时它需要构建我的表。然后我使用该数据库 当我创建新的前端时。以下是我在元素实体中注释新集合的方式:
@CollectionOfElements(fetch=FetchType.EAGER,targetElement = ElementCategory.class)
@JoinTable(name = "Element_Category",joinColumns =
{@JoinColumn(name = "type", referencedColumnName="type"
@JoinColumn(name = "value", referencedColumnName="value")
})
@Column(name="category")
@Enumerated(EnumType.STRING)
public List<ElementCategory> getCategories() {
return categories;
}
问题是,当我启动服务器时,创建的元素表对(类型,值)具有唯一约束,这是不正确的,可以有多个元素相同的类型和值字段,它们只是属于相同的类别或类别,具体取决于 element_category 表的外观。
我知道我所设置的是多对多,但我不能使用多对多,除非我的元素类别字段包含类别实体的集合,但它不仅仅是一组枚举。
这可以用 hibernate 来实现吗?
继这个问题之后,我尝试了另一种方法并创建了 ElementCategory 实体:
@Entity
@Table(name="ElementCategory")
public class ElementCategory implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id =id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int hash = 1;
hash = hash * (17 + id);
hash = hash * 31 + description.hashCode();
return hash;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(!super.equals(obj)) {
return false;
}
if(getClass() != obj.getClass()) {
return false;
}
ElementCategory other = (ElementCategory)obj;
return ((other.id == id) && (this.description.equals(other.description)));
}
}
在我的 Element 实体类中映射它,如下所示:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "ELEMENT_ELEMENTCATOGORY", joinColumns = {
@JoinColumn(name = "type", referencedColumnName = "type"),
@JoinColumn(name = "value", referencedColumnName = "value") })
@Column(name = "category")
public List<ElementCategory> getCategories() {
return categories;
}
我尝试将 $unique=false$ 放在整个商店中,但生成的表仍然在我的元素表:
CONSTRAINT element_type_value_key UNIQUE (type , value )
我不希望它是唯一的,多个元素可以具有该配置。
This is a strange scenario born of the fact that I am working with a legacy system who's structure I cannot change.
The scenario is that I have a Element table that contains various data about an element with to important fields "type" and a "value".
Various combinations of type and value are categorised. For instance, all Elements of type 2 and value "green" belong to category 1.
I'm building a user interface in to the legacy system that lets users define these categories given a type and and a value (obviously). What I want is for the legacy system to include a set of Categories in it's hibernate Entity. a Category is actually an Enum of type ElementCategory, one of my remits is that the legacy app has to be the one to define my schema so it needs to build up my tables when I fire it up. and then I use that database
when I create my new front end. Here's how I've annotated my new collection within my Element Entity:
@CollectionOfElements(fetch=FetchType.EAGER,targetElement = ElementCategory.class)
@JoinTable(name = "Element_Category",joinColumns =
{@JoinColumn(name = "type", referencedColumnName="type"
@JoinColumn(name = "value", referencedColumnName="value")
})
@Column(name="category")
@Enumerated(EnumType.STRING)
public List<ElementCategory> getCategories() {
return categories;
}
The problem is that when I fire up the server the Element table that's created has a unique constraint on (type, value), which is incorrect, there can be multiple Elements with the same type and value fields, they just belong to the same category OR categories, depending on how the element_category table looks.
I know that what I have is a many to many set up, but I can;t use many to many unless my Elements category field contains a collection of Category Entities, but it doesn't it's just a set of enums.
Can this be achieved with hibernate at all?
Following on from this issues I tried another approach and created the ElementCategory Entity:
@Entity
@Table(name="ElementCategory")
public class ElementCategory implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id =id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int hash = 1;
hash = hash * (17 + id);
hash = hash * 31 + description.hashCode();
return hash;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}
if(!super.equals(obj)) {
return false;
}
if(getClass() != obj.getClass()) {
return false;
}
ElementCategory other = (ElementCategory)obj;
return ((other.id == id) && (this.description.equals(other.description)));
}
}
mapping this in my Element Entity class like this:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "ELEMENT_ELEMENTCATOGORY", joinColumns = {
@JoinColumn(name = "type", referencedColumnName = "type"),
@JoinColumn(name = "value", referencedColumnName = "value") })
@Column(name = "category")
public List<ElementCategory> getCategories() {
return categories;
}
I have tried putting $unique=false$ all over the shop but the generated tables still end up with this in my Element table:
CONSTRAINT element_type_value_key UNIQUE (type , value )
I don't want it to be unique, multiple element can have that configuration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以尝试这样的事情:
you could try something like this: