我的POJO Student.java應該是什麼在Hibernate連接表
package foo;
public class Student {
private int student_id;
private String name;
private College college;
Student(){}
public Student(String name,College college){
this.name=name;
this.college=college;
}
public void setCollege(College college) {
this.college = college;
}
public College getCollege() {
return college;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setStudentId(int id) {
this.student_id = id;
}
public int getStudentId() {
return student_id;
}
}
College.java
package foo;
public class College {
private String college_code;
private String name;
private String city;
College(){}
public College(String college_code,String name ,String city){
this.setCollegeCode(college_code);
this.setName(name);
this.setCity(city);
}
public void setCollegeCode(String college_code) {
this.college_code = college_code;
}
public String getCollegeCode() {
return college_code;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}
}
我的映射文件的方法。 College.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">
<!-- Generated Dec 2, 2015 11:44:19 AM by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
<class name="foo.College" table="college" catalog="Students">
<id name="collegeCode" type="string">
<column name="college_code" length="5" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="name" length="50" />
</property>
<property name="city" type="string">
<column name="city" length="20" />
</property>
</class>
</hibernate-mapping>
Student.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">
<!-- Generated Dec 2, 2015 11:44:19 AM by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
<class name="foo.Student" table="student" catalog="Students">
<id name="studentId" type="int">
<column name="student_id" />
<generator class="increment" />
</id>
<property name="name" type="string">
<column name="name" length="30" />
</property>
<many-to-one name="college" column="college_code"
class="foo.College" not-null="true"/>
</class>
</hibernate-mapping>
和主類
Main.java
package utils;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.sql.ordering.antlr.Factory;
import foo.College;
import foo.Student;
public class Main {
public static SessionFactory factory;
public static void main(String[] args) {
try{
factory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
Main obj=new Main();
//College clg1= obj.addCollege("00000","RKGIT","GHAZIABAD"); //Add a college to start with
//College clg1=obj.listCollege("00000");
//Integer std1= obj.addStudent("Vishal Tyagi",clg1); //add students
//System.out.println(std1);
obj.listStudents();
}
//Add college
private College addCollege(String code,String name,String city){
Session session =factory.openSession();
Transaction tx;
tx=session.beginTransaction();
College clg1=new College(code,name,city);
session.save(clg1);
tx.commit();
return clg1;
}
private Integer addStudent(String name,College college)
{
Session session =factory.openSession();
Transaction tx=session.beginTransaction();
Student std1=new Student(name,college);
Integer id1=(Integer)session.save(std1);
tx.commit();
return id1;
}
private College listCollege(String code){
Session session =factory.openSession();
Transaction tx=session.beginTransaction();
Query query =session.createQuery("from College where college_code=:code");
query.setParameter("code", code);
List list=query.list();
if(!list.isEmpty())
{
for(Object a :list)
return (College)a;
}
return null;
}
/*private void listStudents(){
Session session =factory.openSession();
Transaction tx=session.beginTransaction();
String querystring="from Student";
Query query=session.createQuery(querystring);
List list= query.list();
for(Object a:list)
{
Student student=(Student)a;
System.out.println("Name : "+student.getName());
System.out.println("ID : "+student.getStudentId());
College college=student.getCollege();
System.out.println("Collegecode :"+college.getCollegeCode());
System.out.println("Collegename :"+college.getName());
System.out.println("collegecity :" +
""+college.getCity());
System.out.println("--------------------------------");
}
}*/
private void listStudents(){
Session session =factory.openSession();
Transaction tx=session.beginTransaction();
String querystring="from Student a,College b where a.college.getCollegeCode()=b.college_code";
Query query=session.createQuery(querystring);
List list=query.list();
for(Object a :list){
System.out.println(a.toString());
}
tx.commit();
}
}
我可以通過註釋listStudents方法列出學生,但是,如何在Hibernate中將這兩個表連接在一起?
這種方法listStudents給出了錯誤:「無法解析財產」
我只是走錯了路,我的評論listStudents方法將成爲我的目的或有任何方式加入這兩個表的學生和大學。
你可以使用註解保證xml映射。更容易理解和分析。 – Antoniossss
您在查詢中使用getter而不是屬性collegeCode。這是錯誤的 –
@Simo將querystring更改爲:「 - 從學生a,學院b,其中a.college.college_code = b.college_code」,並獲得相同的錯誤。 –