Hibernate4 .1.2 Annotation实例

步骤
(1)添加jar包,尤其要找对oracle驱动jar包

(2)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>
<property name="connection.username">testuser</property>
<property name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:ORCL
</property>
<property name="dialect">
org.hibernate.dialect.Oracle10gDialect
</property>
<property name="connection.password">testuser</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.autocommit">false</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
</session-factory>

</hibernate-configuration>


(3)建立实体对象


package com.neal.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class C_user implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private Date birthday;

@Id @GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(length=10)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Temporal(TemporalType.DATE)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}


(4)测试


package junit.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.BeforeClass;
import org.junit.Test;

import com.neal.entity.C_user;

public class HibernateTest {

private static SessionFactory sessionFactory;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Configuration cfg = new Configuration();
cfg.configure(); // 重要
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
try {
sessionFactory = cfg.configure()
.addAnnotatedClass(C_user.class)
.buildSessionFactory(sr);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Test
public void test() {

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
C_user user = new C_user();
user.setBirthday(new Date());
user.setName("xiaohong");

session.persist(user);
tx.commit();
session.close();
System.out.println("end");
}

}


(5)结果

15:57:08,208 INFO Version:37 - HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
15:57:08,216 INFO Version:41 - HHH000412: Hibernate Core {4.1.2.Final}
15:57:08,217 INFO Environment:239 - HHH000206: hibernate.properties not found
15:57:08,219 INFO Environment:342 - HHH000021: Bytecode provider name : javassist
15:57:08,237 INFO Configuration:1924 - HHH000043: Configuring from resource: /hibernate.cfg.xml
15:57:08,237 INFO Configuration:1943 - HHH000040: Configuration resource: /hibernate.cfg.xml
15:57:08,272 WARN DTDEntityResolver:74 - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
15:57:08,291 INFO Configuration:2065 - HHH000041: Configured SessionFactory: null
15:57:08,318 INFO Configuration:1924 - HHH000043: Configuring from resource: /hibernate.cfg.xml
15:57:08,318 INFO Configuration:1943 - HHH000040: Configuration resource: /hibernate.cfg.xml
15:57:08,320 WARN DTDEntityResolver:74 - HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
15:57:08,326 INFO Configuration:2065 - HHH000041: Configured SessionFactory: null
15:57:08,440 INFO DriverManagerConnectionProviderImpl:96 - HHH000402: Using Hibernate built-in connection pool (not for production use!)
15:57:08,442 INFO DriverManagerConnectionProviderImpl:129 - HHH000115: Hibernate connection pool size: 10
15:57:08,442 INFO DriverManagerConnectionProviderImpl:132 - HHH000006: Autocommit mode: false
15:57:08,443 INFO DriverManagerConnectionProviderImpl:146 - HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@127.0.0.1:1521:ORCL]
15:57:08,443 INFO DriverManagerConnectionProviderImpl:151 - HHH000046: Connection properties: {user=testuser, password=****, autocommit=false}
15:57:08,465 INFO Dialect:122 - HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
15:57:08,473 INFO LobCreatorBuilder:85 - HHH000422: Disabling contextual LOB creation as connection was null
15:57:08,483 INFO TransactionFactoryInitiator:68 - HHH000399: Using default transaction strategy (direct JDBC transactions)
15:57:08,486 INFO ASTQueryTranslatorFactory:48 - HHH000397: Using ASTQueryTranslatorFactory
15:57:08,651 INFO SchemaUpdate:182 - HHH000228: Running hbm2ddl schema update
15:57:08,651 INFO SchemaUpdate:193 - HHH000102: Fetching database metadata
15:57:09,066 INFO SchemaUpdate:205 - HHH000396: Updating schema
15:57:09,154 INFO TableMetadata:65 - HHH000261: Table found: TESTUSER.C_USER
15:57:09,154 INFO TableMetadata:66 - HHH000037: Columns: [id, birthday, name]
15:57:09,155 INFO TableMetadata:68 - HHH000108: Foreign keys: []
15:57:09,155 INFO TableMetadata:69 - HHH000126: Indexes: [sys_c0010874]
15:57:09,155 INFO SchemaUpdate:240 - HHH000232: Schema update complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into C_user (birthday, name, id) values (?, ?, ?)
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值