hibernate5.3 tutorials
所属分类 hibernate
浏览量 512
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.cfg.xml
org.h2.Driver
jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE
sa
1
org.hibernate.dialect.H2Dialect
org.hibernate.cache.internal.NoCacheProvider
true
create
hbm 与 注解 方式的区别
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