Hibernate annotation更多的使用JPA Annotation,只有在JPA没有定义或者有特殊的要求的使用才使用Hibernate自己的Annotation。本文主要写如何通过Hibernate Annotation开始自己的应用程序。
1.Hibernate配置
在Hibernate配置中,主要是Mapping一项中 <mapping class="org.cookie.study.hibernate.GaGaUser"/> class替代之前的resource
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://192.168.1.108/test</property>
<property name="connection.username">root</property>
<property name="connection.password">dong890418</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</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">update</property>
<mapping class="org.cookie.study.hibernate.GaGaUser"/>
</session-factory>
</hibernate-configuration>
2.实体类的注解
实体类注解主要采用JPA注解
package org.cookie.study.hibernate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="ano_gaga_user")
public class GaGaUser {
private Long id;
private String name;
private String password;
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "GaGaUser [id=" + id + "]";
}
}
3.在程序中使用Hibernate Annotation
在Hibernate 4.0使用Annotation和hibernate xml Mapping是一样的用法。
package org.cookie.study.hibernate;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateStudyAn {
@SuppressWarnings("unchecked")
public static void main(String[] arg){
Configuration configuration = new Configuration().configure("hibernate_an.cfg.xml");
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = serviceRegistryBuilder.buildServiceRegistry();
SessionFactory factory = configuration.buildSessionFactory(serviceRegistry );
Session session = factory.openSession();
session.beginTransaction();
GaGaUser gagaUser = new GaGaUser();
gagaUser.setName("user");
gagaUser.setPassword("1");
session.save(gagaUser);
session.getTransaction().commit();
session.beginTransaction();
List gagaUsers = session.createQuery("from GaGaUser").list();
for (GaGaUser user : (List<GaGaUser>)gagaUsers) {
System.out.println(user);
}
session.getTransaction().commit();
}
}