gpt4 book ai didi

java - hibernate 复合键不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 22:20:34 25 4
gpt4 key购买 nike

这是我使用的代码:

数据库:

 CREATE TABLE IF NOT EXISTS `btech_faculty_assigned` (
`subject_id` varchar(8) NOT NULL,
`year` varchar(4) NOT NULL,
`section` varchar(1) NOT NULL,
`branch` varchar(10) NOT NULL,
`semister` varchar(1) NOT NULL,
`FID` varchar(10) NOT NULL,
`islab` varchar(1) NOT NULL,
PRIMARY KEY (`subject_id`,`year`,`section`,`branch`,`semister`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

hbm 文件:

<?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.att_marks.students.BtechFacultyAssigned" table="btech_faculty_assigned">
<composite-id>
<key-property name="subjectID" column="subject_id"/>
<key-property name="year" column="year"/>
<key-property name="section" column="section"/>
<key-property name="branch" column="branch"/>
<key-property name="semister" column="semister"/>
</composite-id>

<property name="FID" column="FID"></property>
<property name="islab" column="islab"></property>
</class>
</hibernate-mapping>

POJO文件:

package com.att_marks.students;

public class BtechFacultyAssigned {
private String subjectID;
public String getSubjectID() {
return subjectID;
}
public void setSubjectID(String subjectID) {
this.subjectID = subjectID;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getSeciton() {
return section;
}
public void setSeciton(String section) {
this.section = section;
}
public String getFID() {
return FID;
}
public void setFID(String fID) {
FID = fID;
}
public String getSemister() {
return semister;
}
public void setSemister(String semister) {
this.semister = semister;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public String getIslab() {
return islab;
}
public void setIslab(String islab) {
this.islab = islab;
}
private String year;
private String section;
private String FID;
private String semister;
private String branch;
private String islab;

}

只要这个 hbm 文件不包含在配置文件中,整个项目就可以正常工作。当这个文件包含在配置文件中时,sessionFactory.createQuery(query) 文件会抛出空指针异常。

最佳答案

Hibernate doc说:

A table with a composite key can be mapped with multiple properties of the class as identifier properties. The element accepts property mappings and mappings as child elements.

The persistent class must override equals() and hashCode() to implement composite identifier equality. It must also implement Serializable.

即使这可能不是您问题的原因(但行为看起来像),您也必须遵循此要求并添加 equalshashcode可串行化

关于java - hibernate 复合键不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4629163/

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