我陷入了这个问题。数据库架构是由其他人提供的,因此我不能简单地更改名称。我尝试在各处添加适当的注释,也许我遗漏了一些(显而易见的)?
这是我的完整映射(很多类),我将省略getter / setter。
问题是当hibernate试图获得全部 List<ControlRuleAttrib> controlRuleAttribs
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; }
ControlRuleAttrib
@Table(name = "CONTROL_RULE_ATTRIB") @Entity public class ControlRuleAttrib { @EmbeddedId private ControlRuleAttribPK controlRuleAttribPK; @Column(name = "ATTRIBUTE_VALUE") private String attributeValue; }
ControleRuleAttribPK 这里的问题是,是否有可能以某种方式得到实体ControlRuleAttribType的ControlRuleAttrib?如您所见,下面ControlRuleAttribTypeId是IDControleRuleAttribType。我想得到整个对象的等值整数。
ControlRuleAttribType
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; }
ControleRuleType
@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; }
编辑
这是stacktrace:
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()声明的方法:
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; }
并hibernate请求:
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=?应为controlrul0_.CONTROL_RULE_ID=?。
controlrul0_.CONTROL_RULE_CONTROL_RULE_ID=?
controlrul0_.CONTROL_RULE_ID=?
无论如何,我可以使它工作吗?:/
经过数小时的努力,我终于使它在我的项目中正常工作。我所做的是这样的:
@OneToMany(fetch = FetchType.LAZY, mappedBy = "controlRuleAttribPK.controlRuleId") @Cascade(CascadeType.ALL) private List<ControlRuleAttrib> controlRuleAttribs;
基本上指出集合应该使用复合主键中的controlRuleId。到目前为止,它的工作很棒!