标签:
参考:Hibernate 5.0 http://docs.jboss.org/hibernate/orm/5.0/quickstart/html/
hibernate.cfg.xml
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --> <!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.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property> <property name="connection.username">myname</property> <property name="connection.password">mypassword</property> <!-- 内置的数据库连接池配置(不推荐使用内置的数据库连接池) --> <property name="connection.pool_size">1</property> <!-- SQL方言配置 --> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <!-- 不设二级缓存 --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- 打印SQL到标准输出 --> <property name="show_sql">true</property> <!-- create:启动时删除旧表重建新表 -->
<!-- create-drop:同上,不过关闭Hibernate时会删除表 -->
<!-- update:启动时验证表,若不一致会更新表 -->
<!-- validate:启动时验证表,若不一致抛异常,不会更新表(推荐) --> <property name="hbm2ddl.auto">create</property>
<!-- 使用XML映射实体到数据库 --> <mapping resource="org/hibernate/tutorial/hbm/Event.hbm.xml"/>
<!-- 使用注解映射实体到数据库 -->
<mapping class="org.hibernate.tutorial.annotations.Event"/> </session-factory> </hibernate-configuration>
Java实体类
1. 使用标准JavaBean中对属性的传统命名方式,并有相应的getter和setter方法,属性私有
2. 无参的构造方法
Classname.hbm.xml
<?xml version="1.0"?> <!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --> <!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.elenore.entity.hbm"> <class name="Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="increment"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/>
<!-- 不写type,则默认和声明的属性类型的默认匹配类型一样 -->
<!-- 不写column,则默认和name值一样 -->
<!-- 考虑到启动性能,应该显示说明属性的匹配类型(因为hibernate的自动匹配是通过反射机制) --> <property name="title"/> </class> </hibernate-mapping>
使用注解映射
@Entity @Table( name = "EVENTS" ) public class Event { private Long id; private String title; private Date date; public Event() { // this form used by Hibernate } public Event(String title, Date date) { // for application use, to create new events this.title = title; this.date = date; } @Id @GeneratedValue(generator="increment") @GenericGenerator(name="increment", strategy = "increment") public Long getId() { return id; } private void setId(Long id) { this.id = id; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "EVENT_DATE") public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
Hibernate启动
protected void setUp() throws Exception { // SessionFactory在一个应用中只会被创建一次 final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // 从hibernate.cfg.xml解析配置 .build(); try { sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); } catch (Exception e) { // registry会被SessionFactory自动销毁 // 但是如果在创建SessionFactory时有异常,则需要手动销毁 StandardServiceRegistryBuilder.destroy( registry ); } }
Hibernate使用
// 插入
Session session = sessionFactory.openSession(); session.beginTransaction(); session.save( new Event( "Our very first event!", new Date() ) ); session.save( new Event( "A follow up event", new Date() ) ); session.getTransaction().commit(); session.close(); // 查询 session = sessionFactory.openSession(); session.beginTransaction(); List result = session.createQuery( "from Event" ).list(); for ( Event event : (List<Event>) result ) { System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() ); } session.getTransaction().commit(); session.close();
标签:
原文地址:http://www.cnblogs.com/hippiebaby/p/5074499.html