Hibernate / JPA A collection of elements with many for many relationships?

This is a strange scenario related to the fact that I am working with an outdated system, the structure of which I cannot change.

The scenario is that I have an Element table that contains various data about an element with the important fields "type" and "value".

Various combinations of type and value are classified. For example, all elements of type 2 and the value green are in category 1.

I am creating a user interface for an outdated system that allows users to define these categories based on type and value (obviously). I want the legacy system to include a set of categories in her sleeping Entity. The category is actually an enumeration of type ElementCategory, one of my tasks is that the legacy application should be such as to define my schema, so I need to collect my tables when I run it. and then I use this database when I create my new interface. Here's how I annotated my new collection in my Entity 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;
}

, , Element (, ), , , , , element_category.

, , , "" , .

?

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

}

Entity Entity :

    @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 $ , - Element:

CONSTRAINT element_type_value_key UNIQUE (type , value )

, , ​​.

+2
1

- :

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

All Articles