package com.qian.domain;
import java.util.Date;
public class User2 {
private String user2Id;
private String user2Name;
private Date user2CreateDate;
public String getUser2Id() {
return user2Id;
}
public void setUser2Id(String user2Id) {
this.user2Id = user2Id;
}
public String getUser2Name() {
return user2Name;
}
public void setUser2Name(String user2Name) {
this.user2Name = user2Name;
}
public Date getUser2CreateDate() {
return user2CreateDate;
}
public void setUser2CreateDate(Date user2CreateDate) {
this.user2CreateDate = user2CreateDate;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.qian.domain">
<class name="User2" table="t_User2">
<id name="user2Id" column="User2_ID">
<generator class="uuid"/>
</id>
<property name="user2Name"/>
<property name="user2CreateDate"/>
</class>
</hibernate-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
hibernate.cfg.xml文件
<?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">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
<property name="connection.username">scott</property>
<property name="connection.password">qian</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<property name="show_sql">true</property>
<mapping resource="com/qian/domain/User.hbm.xml"/>
<mapping resource="com/qian/domain/User2.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
package com.qian.envir;
import java.util.EnumSet;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
public class ExoprotDB {
public static void main(String[] args) {
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
MetadataImplementor metadataImplementor = (MetadataImplementor) new MetadataSources(serviceRegistry)
.buildMetadata();
SchemaExport export = new SchemaExport();
export.create(EnumSet.of(TargetType.DATABASE), metadataImplementor);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
package com.qian.envir;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static{
final StandardServiceRegistry standardServiceRegistry = new
StandardServiceRegistryBuilder().configure().build();
try{
sessionFactory = new MetadataSources(standardServiceRegistry)
.buildMetadata().buildSessionFactory();
}catch(Exception e){
StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
}
public static void freeSession(Session session){
if(session!=null){
if(session.isOpen()){
session.close();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41