package com.lwp.hibernate.Model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
public Customer(){}
public Customer(String name){
this.name=name;
}
@Id
@GeneratedValue
@Column(name="ID")
private Long id;
@Column(name="NAME",length=15)
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.lwp.hibernate.Model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="ORDERS")
public class Order implements Serializable{
private static final long serialVersionUID = 1L;
public Order(){}
public Order(String orderNumber,Customer customer){
this.orderNumber =orderNumber;
this.customer=customer;
}
@Id
@GeneratedValue
@Column(name="ID")
private Long id;
@Column(name="ORDER_NUMBER",length=15)
private String orderNumber;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="CUSTOMER_ID", nullable = false)
private Customer customer;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import com.lwp.hibernate.Model.Customer;
import com.lwp.hibernate.Model.Order;
public class CustomerTest {
public static void main(String[] args) {
new SchemaExport(new AnnotationConfiguration().configure("/hibernate.cfg.xml")).create(true, false);
Customer customer = new Customer("Tom");
Order order1 = new Order("Tom_Order001",customer);
Order order2 = new Order("Tom_Order002",customer);
save(customer);
save(order1);
save(order2);
}
public static void save(Object o){
//Configuration cfg = new Configuration();//xx.hbm.xml形式下的new
Configuration cfg = new AnnotationConfiguration(); //Annotation形式下的new
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(o);
session.getTransaction().commit();
session.close();
sf.close();
}
}
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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect 方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!--<property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout 打印sql语句 -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup 让hibernate自动生成建表语句 -->
<!-- validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构 -->
<property name="hbm2ddl.auto">update</property>
<!-- xx.hbm.xml形式
<mapping resource="com/lwp/hibernate/Model/Customer.hbm.xml"/>
<mapping resource="com/lwp/hibernate/Model/Order.hbm.xml"/>
-->
<!-- Annotation形式 -->
<mapping class="com.lwp.hibernate.Model.Customer"/>
<mapping class="com.lwp.hibernate.Model.Order"/>
</session-factory>
</hibernate-configuration>
SQL:
alter table ORDERS drop foreign key FK8B7256E5C1E4F651
drop table if exists Customer
drop table if exists ORDERS
create table Customer (ID bigint not null auto_increment, NAME varchar(15), primary key (ID))
create table ORDERS (ID bigint not null auto_increment, ORDER_NUMBER varchar(15), CUSTOMER_ID bigint not null, primary key (ID))
alter table ORDERS add index FK8B7256E5C1E4F651 (CUSTOMER_ID), add constraint FK8B7256E5C1E4F651 foreign key (CUSTOMER_ID) references Customer (ID)
Hibernate: insert into Customer (NAME) values (?)
Hibernate: insert into ORDERS (CUSTOMER_ID, ORDER_NUMBER) values (?, ?)
Hibernate: insert into ORDERS (CUSTOMER_ID, ORDER_NUMBER) values (?, ?)