我被这个问题困住了。数据库模式是由其他人提供的,所以我不能简单地更改名称。我尝试在任何地方添加适当的注释,也许我遗漏了一些东西(很明显)?
这是我的完整映射(很多类),我将省略 getter/setter。
问题是当休眠试图获取所有List<ControlRuleAttrib> controlRuleAttribs
控制规则
@Entity
@Table(name = "CONTROL_RULE")
public class ControlRule implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CONTROL_RULE_ID")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@Cascade(CascadeType.ALL)
@JoinColumn(name = "CONTROL_RULE_TYPE_ID")
@ForeignKey(name = "CONTROL_RULE_TYPE_ID")
private ControlRuleType controlRuleType;
@Column(name = "JOB_NM")
private String jobname;
@Column(name = "LIBRARY_NM")
private String libraryname;
@Column(name = "TABLE_NM")
private String tablename;
@Column(name = "COLUMN_NM")
private String columnname;
@OneToMany(fetch = FetchType.LAZY)
@Cascade(CascadeType.ALL)
@JoinTable(name = "CONTROL_RULE_ATTRIB", joinColumns = {
@JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false)
})
private List < ControlRuleAttrib > controlRuleAttribs;
}
控制规则属性
@Table(name = "CONTROL_RULE_ATTRIB")
@Entity
public class ControlRuleAttrib {
@EmbeddedId
private ControlRuleAttribPK controlRuleAttribPK;
@Column(name = "ATTRIBUTE_VALUE")
private String attributeValue;
}
ControleRuleAttribPK
这里的问题是,是否有可能以某种方式从中获取ControlRuleAttribType
实体ControlRuleAttrib
?正如您在下面看到ControlRuleAttribTypeId
的是ControleRuleAttribType
. 我想得到整数的整个对象。
@Embeddable
public class ControlRuleAttribPK implements Serializable {
@Column(name = "CONTROL_RULE_ID")
private Long controlRuleId;
@Column(name = "ATTRIBUTE_SEQ_NUM")
private Integer attributeSeqNum;
@Column(name = "CONTROL_RULE_ATTRIB_TYPE_ID")
private Integer controlRuleAttribTypeId;
}
控制规则属性类型
@Entity
@Table(name = "CONTROL_RULE_ATTRIB_TYPE")
public class ControlRuleAttribType implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CONTROL_RULE_ATTRIB_TYPE_ID")
private Integer id;
@Column(name = "CONTROL_RULE_ATTRIB_TYPE_NM")
private String typename;
@Column(name = "CONTROL_RULE_ATTRIB_TYPE_DESC")
private String typedesc;
@ManyToOne(fetch = FetchType.LAZY)
@Cascade(CascadeType.ALL)
@JoinColumn(name = "CONTROL_RULE_TYPE_ID")
@ForeignKey(name = "CONTROL_RULE_TYPE_ID")
private ControlRuleType controlruletype;
}
控制规则类型
@Entity
@Table(name = "CONTROL_RULE_TYPE")
public class ControlRuleType implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CONTROL_RULE_TYPE_ID")
private Integer id;
@Column(name = "CONTROL_RULE_TYPE_NM")
private String typename;
@Column(name = "CONTROL_RULE_TYPE_DESC")
private String typedesc;
}
编辑
这是堆栈跟踪:
https://gist.github.com/a30dd9ce534d96bb9a97
你会发现,它在这里失败了:
在 com.execon.controllers.main.MainPageController.getMainPage(MainPageController.java:33) [类:]
就是这样:
List<ControlRule> list = SessionFactoryUtils.openSession(
sessionFactory ).createQuery( "from ControlRule" ).list();
System.out.println( list );
我添加的每个映射对象都有toString()
这样声明的方法:
@Override
public String toString()
{
String s = "ControlRule{";
s += "id=" + id.toString();
s += ", controlRuleType=" + controlRuleType;
s += ", jobname='" + jobname + '\'';
s += ", libraryname='" + libraryname + '\'';
s += ", tablename='" + tablename + '\'';
s += ", columnname='" + columnname + '\'';
s += ", controlRuleAttribs=" + controlRuleAttribs;
s += '}';
return s;
}
和休眠请求:
https://gist.github.com/c8584113522757a4e0d8/4f31dc03e7e842eef693fa7ba928e19d27b3ca26
请帮忙 :)
编辑 2
好吧,在阅读了@Jens 的答案后,我对代码进行了一些更改。首先我按照你写的做了,它给出了错误:
org.hibernate.AnnotationException:从 com.execon.models.controlrules.ControlRule 引用 com.execon.models.controlrules.ControlRuleAttrib 的外键具有错误的列数。应该是 3
我想这是对的,因为我有复合主键。
然后我这样尝试:
@OneToMany(fetch = FetchType.LAZY)
@Cascade(CascadeType.ALL)
@JoinTable(name = "CONTROL_RULE_ATTRIB",
joinColumns = {
@JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false)
},
inverseJoinColumns = {
@JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false),
@JoinColumn(name = "CONTROL_RULE_ATTRIB_TYPE_ID", nullable = false, updatable = false),
@JoinColumn(name = "ATTRIBUTE_SEQ_NUM", nullable = false, updatable = false)
})
private List<ControlRuleAttrib> controlRuleAttribs;
非常接近,但它给了我以下例外:
集合映射中的重复列..
所以最后我删除了
joinColumns =
{
@JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false)
}
除了当我尝试到达集合时,所有的东西都编译了,Hibernate 正在执行以下查询:
https://gist.github.com/c88684392f0b7a62bea5
最后一行是controlrul0_.CONTROL_RULE_CONTROL_RULE_ID=?
while it should be controlrul0_.CONTROL_RULE_ID=?
。
无论如何我可以让它工作吗?:/