gpt4 book ai didi

java - org.hibernate.AnnotationException : A Foreign key refering has the wrong number of column. 应该是 2

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:58:50 26 4
gpt4 key购买 nike

Table details

我有上面截图中的表格

类的写法如下

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

注意没有添加getter和setter

测试代码如下

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

我得到以下异常

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)

我开始知道我应该在 FilterAttribute 类中有 2 个属性来保存复合键。但是我们该怎么做呢?

最佳答案

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

它认为您的 ObjectAttribute 有 2 个 ids 复合键

更新:如果它真的有复合多列主键,你应该引用两列

关于java - org.hibernate.AnnotationException : A Foreign key refering has the wrong number of column. 应该是 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24052580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com