主表配置
package com.tang.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="users")
public class Users {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int uid;
private String uname;
private String usex;
private String uaddress;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUsex() {
return usex;
}
public void setUsex(String usex) {
this.usex = usex;
}
public String getUaddress() {
return uaddress;
}
public void setUaddress(String uaddress) {
this.uaddress = uaddress;
}
public Users(String uname, String usex, String uaddress) {
super();
this.uname = uname;
this.usex = usex;
this.uaddress = uaddress;
}
public Users() {
super();
}
}
从表配置
package com.tang.pojo;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="orders")
public class Orders {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int oid;
@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER,targetEntity=Users.class)
@JoinColumn(name="uid" ,nullable=false)
private Users users;
public int getOid() {
return oid;
}
public void setOid(int oid) {
this.oid = oid;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public Orders(Users users) {
super();
this.users = users;
}
public Orders() {
super();
}
}
hibernate.cfg.xml配置
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3308/hib_demo1?useUnicode=true&characterEncoding=utf8</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="myeclipse.connection.profile">mysql-1</property>
<property name="show_sql">true</property>
<mapping class="com.tang.pojo.Orders" />
<mapping class="com.tang.pojo.Users"/>
</session-factory>
</hibernate-configuration>
HibernateSessionFactory配置