Hibernate关联查询


项目框架数据库表

Hibernate关联查询Hibernate关联查询Hibernate关联查询Hibernate关联查询

分析:两个表之间的关联查询需要两个工具类,两个工具类对应两个映射文件,这也是hibernate的缺点,几个工具类就需要对应多少工具类
同时还需要将两个类互相注入也就是user要注入到phone中,phone也需要注入到user中
同时在这其中如出现错误大部分是映射文件配置出错,需要注意映射文件的名称,需要的属性。

代码块
User.java
package dao;
import java.sql.Date;

public class User {
private String idcard;
private String name;
private String password;
private String age;
private String addr;
Phone phone;

public Phone getPhone() {
return phone;
}

public void setPhone(Phone phone) {
this.phone = phone;
}

public User() {
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getIdcard() {
return idcard;
}

public void setIdcard(String idcard) {
this.idcard = idcard;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getAddr() {
return addr;
}

public void setAddr(String addr) {
this.addr = addr;
}
}

User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="dao">
<class name="User" table="t_person">
<id name="idcard" column="idcard">
<generator class="native" />
</id>
<property name="name" column="name" />
<property name="password" column="password" />
<property name="age" column="age" />
<property name="addr" column="addr" />
<one-to-one name="phone" cascade="all" />
</class>
</hibernate-mapping>

Phone.java
package dao;

public class Phone {
String idcard;
String phone;
User user;

public String getIdcard() {
return idcard;
}

public void setIdcard(String idcard) {
this.idcard = idcard;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
}

Phone.hbm.xml
<?xml version="1.0"?>
<!-- 与javabean同包 -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="dao">
<class name="Phone" table="t_phone">
<id name="idcard">
<generator class="native" />
</id>
<property name="phone" column="phone" />
<many-to-one name="user" column="idcard" unique="true"
insert="false" update="false" not-null="true" />
</class>
</hibernate-mapping>

hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 通常,一个session-factory节点代表一个数据库 -->
<session-factory>
<!-- 1. 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///zdy?useUnicode=true&amp;characterEncoding=UTF8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--
数据库方法配置,hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 2. 其他相关配置 -->
<!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 2.2 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 2.3 自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 3. 加载所有映射 -->
<mapping resource="dao/User.hbm.xml"/>
<mapping resource="dao/Phone.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Test.java
package dao;

import java.sql.Date;
import java.util.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {
public static void main(String[] args) {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hql = "FROM User u,Phone t WHERE u.idcard=t.idcard ";
Query query = session.createQuery(hql);
List list = query.list();
System.out.println(list.toString());
//
tx.commit();
session.close();
// sessionFactory.close();// 一定 不 要 关闭.这里只是写出来做例子
}
}