首页  

hibernate5.3 tutorials     所属分类 hibernate 浏览量 524
https://docs.jboss.org/hibernate/orm/5.3/quickstart/html_single/

https://docs.jboss.org/hibernate/orm/5.3/quickstart/html_single/hibernate-tutorials.zip

https://gitee.com/dyyx/hellocode/tree/master/demo/hibernate-tutorials


basic Hibernate tutorial illustrating the use of native APIs and hbm.xml for mapping metadata annotations Hibernate tutorial illustrating the use of native APIs and annotations for mapping metadata entitymanager Hibernate tutorial illustrating the use of JPA APIs and annotations for mapping metadata envers Hibernate tutorial illustrating basic set up and use of Envers
native APIs StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); SessionFactory sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); 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();
JPA APIs EntityManagerFactory entityManagerFactory; // like discussed with regards to SessionFactory, an EntityManagerFactory is set up once for an application // IMPORTANT: notice how the name here matches the name we gave the persistence-unit in persistence.xml! entityManagerFactory = Persistence.createEntityManagerFactory( "org.hibernate.tutorial.jpa" ); EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); entityManager.persist( new Event( "Our very first event!", new Date() ) ); entityManager.persist( new Event( "A follow up event", new Date() ) ); entityManager.getTransaction().commit(); entityManager.close();
package org.hibernate.tutorial.annotations; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.hibernate.annotations.GenericGenerator; @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; } }
Event.hbm.xml <hibernate-mapping package="org.hibernate.tutorial.hbm"> <class name="Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="increment"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title"/> </class> </hibernate-mapping>
hibernate.cfg.xml <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">org.h2.Driver</property> <property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property> <property name="connection.username">sa</property> <property name="connection.password"/> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.H2Dialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping resource="org/hibernate/tutorial/hbm/Event.hbm.xml"/> </session-factory> </hibernate-configuration> hbm 与 注解 方式的区别 <mapping resource="org/hibernate/tutorial/hbm/Event.hbm.xml"/> <mapping class="org.hibernate.tutorial.annotations.Event"/>
hibernate.cfg.xml 配置要点 SQL dialect SQL方言 dialect org.hibernate.dialect.H2Dialect Disable the second-level cache 禁用二级缓存 cache.provider_class org.hibernate.cache.internal.NoCacheProvider show_sql true hbm2ddl.auto create Drop and re-create the database schema on startup

上一篇     下一篇
设计模式之Facade

Java Files 常用方法

hibernate Session get()和load()方法区别

hibernate5 主键生成策略

windows使用技巧

Java identityHashCode