配置文件hibernate.cfg.xml:
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQLDialect
jdbc:mysql://localhost:3306/hubin
root
com.mysql.jdbc.Driver
mysql
true
true
关系对象模型文件主表文件Users.hbm.xml:
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
关系对象模型副表文件Userinf.hbm.xml:
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Java代码:
实体类Users.java:
package com.hubin.entity;
import java.util.HashSet;
import java.util.Set;
/**
* Users entity. @author MyEclipse Persistence Tools
*/
public class Users implements java.io.Serializable {
// Fields
private Long id;
private String username;
private String password;
private String email;
private Set userinfs = new HashSet(0);
// Constructors
/** default constructor */
public Users() {
}
/** full constructor */
public Users(String username, String password, String email, Set userinfs) {
this.username = username;
this.password = password;
this.email = email;
this.userinfs = userinfs;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Set getUserinfs() {
return this.userinfs;
}
public void setUserinfs(Set userinfs) {
this.userinfs = userinfs;
}
}
实体类Userinf.java:
package com.hubin.entity;
/**
* Userinf entity. @author MyEclipse Persistence Tools
*/
public class Userinf implements java.io.Serializable {
// Fields
private Integer id;
private Users users;
private String arm;
// Constructors
/** default constructor */
public Userinf() {
}
/** minimal constructor */
public Userinf(Users users) {
this.users = users;
}
/** full constructor */
public Userinf(Users users, String arm) {
this.users = users;
this.arm = arm;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Users getUsers() {
return this.users;
}
public void setUsers(Users users) {
this.users = users;
}
public String getArm() {
return this.arm;
}
public void setArm(String arm) {
this.arm = arm;
}
}
dao层代码:
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Session;
import com.hubin.entity.Userinf;
import com.hubin.entity.Users;
import com.hubin.sessionfactory.HibernateSessionFactory;
public class UsersDao {
/**
* 获取用户信息表所有数据
*
*/
public static void getUserInfo(){
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
List
list=session.createQuery("from Userinf").list();
System.out.println("集合的数目"+list.size());
for(Userinf uf:list){
System.out.println(uf.getUsers().getUsername()+" "+uf.getArm());
}
session.getTransaction().commit();
session.close();
}
/**
* 获取用户表所有数据信息
*/
public static void getUsers(){
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
List list=session.createQuery("from Users").list();
for(Users us:list){
System.out.println(us.getUsername());
Set set=us.getUserinfs();
for(Userinf uf:set){
System.out.print(uf.getUsers().getEmail()+" ");
}
System.out.println("");
}
System.out.println("************************************************");
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println(it.next().getUsername());
}
session.getTransaction().commit();
session.close();
}
/**
* 获取用户部分信息
*/
public static void getUsersUsernameAndPassword(){
Session session=HibernateSessionFactory.getSession();
session.beginTransaction();
//此时不能通过用户表对象泛型进行接收,只能通过集合对象进行接收
List list=session.createQuery("select username,password from Users").list();
for(int i=0;i //此时取出来的是一个对象数组
Object obj[]=(Object[])list.get(i);
System.out.println(obj[0].toString()+" "+obj[1].toString());
System.out.println("");
}
session.getTransaction().commit();
session.close();
}
public static void main(String args[]){
//调用方法
getUsersUsernameAndPassword();
}
}