Hibernate / JPA元素与多对多关系的集合? [英] Hibernate / JPA Collection of Elements with Many to Many relationship?

查看:115
本文介绍了Hibernate / JPA元素与多对多关系的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这种情况是我有一个Element表格,这是一个奇怪的场景,因为我使用的结构是我无法更改的遗留系统。它包含有关重要字段类型和值的元素的各种数据。



类型和值的各种组合被分类。例如,类型2和值绿色的所有元素都属于类别1。



我正在构建一个用户界面,使用户可以定义这些用户界面给出类型和值的类别(显然)。我想要的是遗留系统在其休眠实体中包含一组类别。一个Category实际上是一个ElementCategory类型的Enum,我的职责之一是传统应用必须是定义我的模式的应用,因此当我启动它时需要建立我的表。然后在创建新的前端时使用该数据库
。以下是我在元素实体中注释我的新集合的方式:

  @CollectionOfElements(fetch = FetchType.EAGER,targetElement = ElementCategory .class)
@JoinTable(name =Element_Category,joinColumns =
{@JoinColumn(name =type,referencedColumnName =type
@JoinColumn(name =value,参考列表名称值)
})
@Column(name =category)
@Enumerated(EnumType.STRING)
public List< ElementCategory> getCategories(){
返回类别;
}

问题是,当我启动服务器创建的元素表对(类型,值)具有唯一的约束,这是不正确的,可以有多个具有相同类型和值字段的元素,它们只属于相同的类别或类别,具体取决于element_category表的外观。



我知道我拥有的是多对多的设置,但我可以使用多对多除非我的元素类别字段包含类别实体的集合,但它不只是一组枚举。



这可以通过hibernate完成吗?



接下来,我尝试了另一种方法并创建了ElementCategory实体:

  @Entity 
@Table(name =ElementCategory)
public class ElementCategory实现Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private int id;

私人字符串描述;

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;


$ b / *(非Javadoc)
* @see java.lang.Object#hashCode()
* /
@Override
public int hashCode(){
int hash = 1;
hash = hash *(17 + id);
hash = hash * 31 + description.hashCode();
返回散列;

$ b $ * b $ b *(非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)));
}

}

在这样的元素实体类中映射它:

  @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(){
返回类别;
}

我试过把$ unique = false $放在店里,但是生成的在我的Element表格中,表格仍然以此结束:

  CONSTRAINT element_type_value_key UNIQUE(类型,值)

我不希望它是唯一的,多个元素可以具有该配置。

  @JoinTable(name =h2_lin>解决方案

Element_Category,
joinColumns = {@JoinColumn(name =type_id)},
inverseJoinColumns = {@JoinColumn(name =elementcategory_id)}

public List< ; ElementCategory> getCategories(){
返回类别;
}


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.

解决方案

you could try something like this:

@JoinTable(name = "Element_Category",
joinColumns = {@JoinColumn(name = "type_id") },
inverseJoinColumns = { @JoinColumn(name = "elementcategory_id") }
)
public List<ElementCategory> getCategories() {
    return categories;
}  

这篇关于Hibernate / JPA元素与多对多关系的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆