chi*_*igk 6 java hibernate hibernate-mapping hibernate-annotations

我有如上面屏幕截图中的表格
课程编写如下
@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;
}
Run Code Online (Sandbox Code Playgroud)
注意没有添加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());
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下例外
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)
Run Code Online (Sandbox Code Playgroud)
我开始知道我应该在FilterAttribute类中拥有2个属性来保存复合键.但是我们怎么能这样做呢?
Sta*_*avL 11
@Entity
public class ObjectAttribute {
@Id
private int id;
@Id <------------------------ try to remove this annotation
@ManyToOne
@JoinColumn(name="objectId")
private Object object;
private String name;
}
Run Code Online (Sandbox Code Playgroud)
它认为你的ObjectAttribute有2个ID复合键
更新:如果它确实有复合多列主键,您应该引用两列
| 归档时间: |
|
| 查看次数: |
17450 次 |
| 最近记录: |