package com.ij34.dao; import javax.persistence.*; @Entity @Inheritance(strategy=InheritanceType.JOINED) @Table(name="people_inf") public class People{ @Id @Column(name="people_id") @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; private String name; private char gender; @Embedded @AttributeOverrides({ @AttributeOverride(name="country" , column=@Column(name="address_country")), @AttributeOverride(name="detail", column=@Column(name="address_detail")), @AttributeOverride(name="zip" , column=@Column(name="address_zip")) }) private Address address; public People(String name,char gender){ this.name=name; this.gender=gender; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
package com.ij34.dao; public class Address{ private String country; private String detail; private String zip; public Address() { // TODO Auto-generated constructor stub } public Address(String country,String detail,String zip){ this.country=country; this.detail=detail; this.zip=zip; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } }
package com.ij34.dao; import javax.persistence.*; @Entity @Table(name="custer_inf") public class Customers extends People{ private String content; @ManyToOne(cascade=CascadeType.ALL,targetEntity=Emplopes.class) @JoinColumn(name="emploee_id",nullable=true) private Emplopes emplopee; public Customers(String name,char gender) { // TODO Auto-generated constructor stub super( name, gender); } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Emplopes getEmplopes() { return emplopee; } public void setEmplopes(Emplopes emplopee) { this.emplopee = emplopee; } }
package com.ij34.dao; import java.util.HashSet; import java.util.Set; import javax.persistence.*; @Entity @Table(name="emplopee_inf") public class Emplopes extends People{ public Emplopes(String name, char gender) { super(name, gender); // TODO Auto-generated constructor stub } private String title; private double salary; @OneToMany(cascade=CascadeType.ALL,targetEntity=Customers.class ,mappedBy="emplopee") private Set<Customers> customers=new HashSet<>(); @ManyToOne(cascade=CascadeType.ALL,targetEntity=Manager.class) @JoinColumn(name="manager_id",nullable=true) private Manager manager; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public Set<Customers> getCustomers() { return customers; } public void setCustomers(Set<Customers> customers) { this.customers = customers; } public Manager getManager() { return manager; } public void setManager(Manager manager) { this.manager = manager; } }
package com.ij34.dao; import java.util.*; import javax.persistence.*; @Entity @Table(name="manager_inf") public class Manager extends Emplopes{ public Manager(String name, char gender) { super( name, gender); // TODO Auto-generated constructor stub } private String department; @OneToMany(cascade=CascadeType.ALL ,targetEntity=Emplopes.class,mappedBy="manager") private Set<Emplopes> emplopee=new HashSet<>(); public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public Set<Emplopes> getEmplopee() { return emplopee; } public void setEmplopee(Set<Emplopes> emplopee) { this.emplopee = emplopee; } }
package com.ij34.web; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.*; import com.ij34.dao.*; public class test01 { public static void main(String[] args)throws Exception { //实例化Configuration Configuration conf=new Configuration().configure(); ServiceRegistry SR=new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build(); // 以Configuration实例创建SessionFactory实例 SessionFactory SF=conf.buildSessionFactory(SR); //create session Session session=SF.openSession(); //start 事务 Transaction tx=session.beginTransaction(); Emplopes li=new Emplopes("li bai", '男'); li.setSalary(14555); li.setTitle("22222222262666asfa"); li.setAddress(new Address("China", "jhaofaofoa", "12.zip")); Emplopes li2=new Emplopes("LI 2", '女'); li2.setTitle("li2 two"); li2.setSalary(222000); li2.setAddress(new Address("UK", "wu li2","li2.zip")); Manager manager=new Manager("li Manager",'女'); manager.setDepartment("JING LI BU"); manager.setSalary(6600); manager.setAddress(new Address("USA", "fsucs", "usa.zip")); li.setManager(manager); Customers c=new Customers("customers", '女'); c.setContent("IIMMM C"); c.setAddress(new Address("China", "im cuseter", "cu.zip")); c.setEmplopes(li); People p=new People("LIN y", 'g'); p.setAddress(new Address("China", "china people", "china.zip")); session.save(manager); session.save(p); session.persist(li); session.persist(li2); session.save(c); tx.commit(); session.close(); SF.close(); } }
<?xml version='1.0' encoding='utf-8'?> <!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> <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">123456</property> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping class="com.ij34.dao.Customers"/> <mapping class="com.ij34.dao.Emplopes"/> <mapping class="com.ij34.dao.People"/> <mapping class="com.ij34.dao.Manager"/> </session-factory> </hibernate-configuration>
**********************************************************
十月 19, 2016 1:22:19 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
十月 19, 2016 1:22:19 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
十月 19, 2016 1:22:19 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十月 19, 2016 1:22:19 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十月 19, 2016 1:22:19 上午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
十月 19, 2016 1:22:19 上午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
十月 19, 2016 1:22:19 上午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
十月 19, 2016 1:22:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
十月 19, 2016 1:22:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate]
十月 19, 2016 1:22:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
十月 19, 2016 1:22:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
十月 19, 2016 1:22:19 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Wed Oct 19 01:22:19 CST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
十月 19, 2016 1:22:19 上午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
十月 19, 2016 1:22:20 上午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
十月 19, 2016 1:22:20 上午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
十月 19, 2016 1:22:20 上午 org.hibernate.tuple.PojoInstantiator <init>
INFO: HHH000182: No default (no-argument) constructor for class: com.ij34.dao.People (class must be instantiated by Interceptor)
十月 19, 2016 1:22:20 上午 org.hibernate.tuple.PojoInstantiator <init>
INFO: HHH000182: No default (no-argument) constructor for class: com.ij34.dao.Customers (class must be instantiated by Interceptor)
十月 19, 2016 1:22:20 上午 org.hibernate.tuple.PojoInstantiator <init>
INFO: HHH000182: No default (no-argument) constructor for class: com.ij34.dao.Emplopes (class must be instantiated by Interceptor)
十月 19, 2016 1:22:20 上午 org.hibernate.tuple.PojoInstantiator <init>
INFO: HHH000182: No default (no-argument) constructor for class: com.ij34.dao.Manager (class must be instantiated by Interceptor)
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: custer_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: emplopee_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: manager_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: people_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: custer_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: emplopee_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: manager_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: people_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: custer_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: emplopee_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: manager_inf
十月 19, 2016 1:22:20 上午 org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: people_inf
十月 19, 2016 1:22:27 上午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: insert into people_inf (address_country, address_detail, address_zip, gender, name) values (?, ?, ?, ?, ?)
Hibernate: insert into emplopee_inf (manager_id, salary, title, people_id) values (?, ?, ?, ?)
Hibernate: insert into manager_inf (department, people_id) values (?, ?)
Hibernate: insert into people_inf (address_country, address_detail, address_zip, gender, name) values (?, ?, ?, ?, ?)
Hibernate: insert into people_inf (address_country, address_detail, address_zip, gender, name) values (?, ?, ?, ?, ?)
Hibernate: insert into emplopee_inf (manager_id, salary, title, people_id) values (?, ?, ?, ?)
Hibernate: insert into people_inf (address_country, address_detail, address_zip, gender, name) values (?, ?, ?, ?, ?)
Hibernate: insert into emplopee_inf (manager_id, salary, title, people_id) values (?, ?, ?, ?)
Hibernate: insert into people_inf (address_country, address_detail, address_zip, gender, name) values (?, ?, ?, ?, ?)
Hibernate: insert into custer_inf (content, emploee_id, people_id) values (?, ?, ?)
十月 19, 2016 1:22:27 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate]
**********************************************
**