23,407
社区成员
发帖
与我相关
我的任务
分享
package com.hibernate.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="person_inf")
public class Person {
@Id
@Column(name="person_id")
@SequenceGenerator(name="seq_pk",sequenceName="seq_pk",initialValue=1,allocationSize=1)
@GeneratedValue(generator="seq_pk",strategy=GenerationType.SEQUENCE)
private Integer id;
private String name;
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;
}
}
package com.hibernate.test;
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.ServiceRegistry;
import com.hibernate.entity.Person;
public class PersonManager {
public static void main(String[] args) {
Configuration conf = new Configuration().configure();
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(sr);
Session ss = sf.openSession();
Transaction t = ss.beginTransaction();
Person p = new Person();
p.setName("zkf");
ss.save(p);
t.commit();
ss.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.username">system</property>
<property name="connection.password">zkf</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521/orcl</property>
<property name="hibernate.dialect">org.hibernate.dialect.TimesTenDialect</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="com.hibernate.entity.School"/>
<mapping class="com.hibernate.entity.Person"/>
</session-factory>
</hibernate-configuration>
create table person_inf (
person_id int primary key,
name varchar(20)
);
create sequence seq_pk start with 1 increment by 1 minvalue 1 nomaxvalue;