Org.hibernate.AnnotationException: foreign key reference has the wrong number of columns. should be 2

Table details

I have tables like in the screenshot above

The class is written below

@Entity  
public class Object {  
    @Id  
    private int id;  

    private String name;  

    @OneToMany(mappedBy="object",fetch=FetchType.LAZY)  
    private List<ObjectAttribute> attrubuteList;  
}  

@Entity  
public class ObjectAttribute {  
    @Id  
    private int id;  
    @Id  
    @ManyToOne  
    @JoinColumn(name="objectId")  
    private Object object;  
    private String name;  
}  

@Entity  
public class Filter {  
    @Id  
    private int filterId;  
    @ManyToOne  
    @JoinColumn(name="ObjectId")  
    private Object object;  
    private String filterName;  
    @OneToMany(mappedBy="filter")  
    private Set<FilterAttribute> filterValues;  
}  

@Entity  
public class FilterAttribute implements Serializable {  

    @Id  
    private int filterAttrId;  
    @Id  
    @ManyToOne  
    @JoinColumn(name="objectId")  
    private Object object;  
    @Id  
    @ManyToOne  
    @JoinColumn(name="filterId")  
    private Filter filter;  
    @Id  
    @ManyToOne  
    @JoinColumn(name="attributeId")  
    private ObjectAttribute attribute;  

    private String value;  
} 

Note: getter and setters not added

and test code below

List<Object> list = sess.createCriteria(Object.class).list();  
        for(Object ob: list)  
        {  
            System.out.println("Object name : "+ ob.getName());  
            List<ObjectAttribute> attList = ob.getAttrubuteList();  

            for (Iterator iterator = attList.iterator(); iterator.hasNext();) {  
                ObjectAttribute objectAttribute = (ObjectAttribute) iterator  
                        .next();  
                System.out.println(objectAttribute.getName());  
            }  
        }  

I get an exception below

Caused by: org.hibernate.AnnotationException: A Foreign key refering test.rest.ObjectAttribute from test.rest.FilterAttribute has the wrong number of column. should be 2  
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:450)  

I found out that I have to have 2 attributes in the FilterAttribute class to store the composite key. But how can we do this?

+4
source share
4 answers
@Entity  
public class ObjectAttribute {  
    @Id  
    private int id;  
    @Id  <------------------------ try to remove this annotation
    @ManyToOne  
    @JoinColumn(name="objectId")  
    private Object object;  
    private String name;  
}  

It believes your ObjectAttribute has 2 composite key identifiers

UPDATE: if it really has a primary key with multiple columns, you must reference both columns

+8

, . @Id , . @ManyToOne @OneToMany, Hibernate -. @Id, .

@Entity  
public class Object {  
    @Id  
    private int id;  

    private String name;  

    @OneToMany(mappedBy="object",fetch=FetchType.LAZY)  
    private List<ObjectAttribute> attrubuteList;  
}  

@Entity  
public class ObjectAttribute {  
    @Id  
    private int id;  

    @ManyToOne  
    @JoinColumn(name="objectId")  
    private Object object;  

    private String name;  
}  

@Entity  
public class Filter {  
    @Id  
    private int filterId;  

    private String filterName;  

    @ManyToOne  
    @JoinColumn(name="ObjectId")  
    private Object object;          

    @OneToMany(mappedBy="filter")  
    private Set<FilterAttribute> filterValues;  
}  

@Entity  
public class FilterAttribute implements Serializable {  
    @Id  
    private int filterAttrId;  

    @ManyToOne  
    @JoinColumn(name="objectId")  
    private Object object;  

    @ManyToOne  
    @JoinColumn(name="filterId")  
    private Filter filter;  

    @ManyToOne  
    @JoinColumn(name="attributeId")  
    private ObjectAttribute attribute;
}
0

,

@Entity  
public class ObjectAttribute {  
    @Id  
    private int id;  
    @Id  
    @ManyToOne  
    @JoinColumn(name="objectId")  
    private Object object;  
    private String name;  
}

2- @id, @IdClass, ( ).

, ,

public class CompositeKey {
    private int id; 
    private Object object;  

    //getters and setter
}

@Entity  
@IdClass(CompositeKey.class)
public class ObjectAttribute {  
    @Id  
    private int id;  
    @Id  
    @ManyToOne  
    @JoinColumn(name="objectId")  
    private Object object;  
    private String name;  
}

It is something like this. But in my experience, this thing is not the best and it is not proposed to use even sleep mode. Just find another solution to use it. Or use your id

Hope that helps

0
source
@Entity  
public class ObjectAttribute {  
@Id  
private int id;  
@Id  
@ManyToOne  
@JoinColumn(name="objectId")  
private Object object;  
private String name;  

}

Use only one @Id object in essence, if you need both, use both column names

0
source

Source: https://habr.com/ru/post/1543341/


All Articles