hibernate 未明确定义列 小记[通俗易懂]

2022-09-27 14:00:59 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

在写关联表的实体类时,用测试代码去运行,出现 16:00:30,817 ERROR JDBCExceptionReporter:72 – ORA-00918: 未明确定义列

16:00:30,833 ERROR PersistSpringImpl:244 – [PersistImpl][find(queryString, offset, length)] hql: from com.esse.projects.cpinfo.entity.MemberList org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not execute query; bad SQL grammar [select memberlist0_.Member_ID as Member1_87_, memberlist0_.MEMBERRECORD_ID as MEMBERRE2_87_, memberlist0_.MEMBER_ID as MEMBER1_87_ from MemberList memberlist0_]; nested exception is java.sql.SQLException: ORA-00918: 未明确定义列

java.sql.SQLException: ORA-00918: 未明确定义列 我的各个相应实体,配置文件如下: 数据库关系图如下:

概念模型

下一:物理模型

1.实体类 public class MemberList extends AbstractEntity implements Serializable { /** * */ private static final long serialVersionUID = 30680140753579543L; private MemberListPK id; private MemberRecord memberRecord; private Member member;

public MemberRecord getMemberRecord() { return memberRecord; } public void setMemberRecord(MemberRecord memberRecord) { this.memberRecord = memberRecord; } public Member getMember() { return member; } public void setMember(Member member) { this.member = member; } public MemberListPK getId() { return id; } public void setId(MemberListPK id) { this.id = id; }

}

2.MemberListPK联合主键实体类 public class MemberListPK implements Serializable { /** * */ private static final long serialVersionUID = 8074810556884497882L; private long memberId; private long memberRecordId;

public long getMemberId() { return memberId; }

public void setMemberId(long memberId) { this.memberId = memberId; }

public long getMemberRecordId() { return memberRecordId; }

public void setMemberRecordId(long memberRecordId) { this.memberRecordId = memberRecordId; }

public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof MemberListPK)) { return false; } MemberListPK castOther = (MemberListPK)other; return new EqualsBuilder() .append(this.getMemberId(), castOther.getMemberId()) .append(this.getMemberRecordId(), castOther.getMemberRecordId()) .isEquals(); }

public int hashCode() { return new HashCodeBuilder() .append(this.getMemberId()) .append(this.getMemberRecordId()) .toHashCode(); }

} 3.hbm.xml文件 <?xml version=”1.0″?> <!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>

<hibernate-mapping> <class name=”com.esse.projects.cpinfo.entity.MemberList” table=”MemberList”>

<composite-id name=”id” class=”com.esse.projects.cpinfo.entity.MemberListPK”> <key-property name=”memberId” type=”long” column=”Member_ID” /> <key-property name=”memberRecordId” type=”long” column=”MEMBERRECORD_ID” /> </composite-id>

<!– Associations –> <many-to-one name=”member” class=”com.esse.projects.cpinfo.entity.Member” insert=”false” update=”false”> <column name=”MEMBER_ID” /> </many-to-one> <many-to-one name=”memberRecord” class=”com.esse.projects.cpinfo.entity.MemberRecord” insert=”false” update=”false”> <column name=”MEMBERRECORD_ID” /> </many-to-one> </class> </hibernate-mapping> 4.在运行测试类时,会出现 未明确定义列,查看hibernat生成的sql语句,发现sql执行的是: select memberlist0_.Member_ID as Member1_87_, memberlist0_.MEMBERRECORD_ID as MEMBERRE2_87_, memberlist0_.MEMBER_ID as MEMBER1_87_ from MemberList memberlist0_ 为什么对于MEMBER_ID字段查询两次呢,想不明白,仔细查看MemberList.hbm.xml文件原来, <key-property name=”memberId” type=”long” column=”Member_ID” /> <many-to-one name=”member” class=”com.esse.projects.cpinfo.entity.Member” insert=”false” update=”false”> <column name=”MEMBER_ID” /> 对于同一个MEMBER_ID字段,两个大小写不一致。导致sql生成的也不一样。 解决方法:把MEMBER_ID字段统一,要不全大写,要不全小写。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/189147.html原文链接:https://javaforall.cn

0 人点赞