Hibernate4有三种实现方式
1. hbm.xml
2. annotaion
3. jpa
这里使用的是annotation放式
关于报错Table.indexes()问题
@Table与@Entity冲突
Jpa中
@Entity
@Table(name = “table_name”)
但是annotation方式就不能这么使用
会出现映射两个表Table.indexes()问题
解决办法:删除@Table(name=“user”)
ABCUser.java
package com.liuhao.hibernate4.demo.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.commons.lang3.builder.ToStringBuilder;
@Entity(name="user")
//@Table(name = "user")
public class ABCUser {
@Id
private int id;
private String name;
private String pwd;
public ABCUser() {
};
public ABCUser(int id, String name, String pwd) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this);
builder.append("id", id);
builder.append("name", name);
builder.append("pwd", pwd);
return builder.toString();
}
}
HIbernateUtil.java
package com.liuhao.hibernate4.demo.uril;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
Configuration cfg = new Configuration();
cfg.configure(); // 重要
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(sr);
} catch (Exception e) {
e.printStackTrace();
}
}
private HibernateUtil() {
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
hibernate.xml
<?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>
<!-- <property name="hibernate.bytecode.use_reflection_optimizer"></property> -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/forum</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="connection.pool_size">10</property>
<mapping class="com.liuhao.hibernate4.demo.entity.ABCUser"></mapping>
</session-factory>
</hibernate-configuration>
JUnit测试类
package com.liuhao.hibernate4.demo.service.test;
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.jboss.logging.Logger;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import com.liuhao.hibernate4.demo.entity.ABCUser;
import com.liuhao.hibernate4.demo.service.UserService;
import com.liuhao.hibernate4.demo.uril.HibernateUtil;
public class UserServiceTest {
private static Logger log = Logger.getLogger(UserService.class);
@After
public void tearDown() throws Exception {
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void test() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
try {
Transaction tx = (Transaction) session.beginTransaction();
ABCUser user = new ABCUser();
user.setId(8);
user.setName("test");
user.setPwd("liuhao");
session.save(user);
log.debug(user);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
sessionFactory.close();
}
System.out.println("end");
}
}
Hibernate4有三种实现方式
1. hbm.xml
2. annotaion
3. jpa
这里使用的是annotation放式
关于报错Table.indexes()问题
@Table与@Entity冲突
Jpa中
@Entity
@Table(name = “table_name”)
但是annotation方式就不能这么使用
会出现映射两个表Table.indexes()问题
解决办法:删除@Table(name=“user”)
ABCUser.java
package com.liuhao.hibernate4.demo.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.commons.lang3.builder.ToStringBuilder;
@Entity(name="user")
//@Table(name = "user")
public class ABCUser {
@Id
private int id;
private String name;
private String pwd;
public ABCUser() {
};
public ABCUser(int id, String name, String pwd) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this);
builder.append("id", id);
builder.append("name", name);
builder.append("pwd", pwd);
return builder.toString();
}
}
HIbernateUtil.java
package com.liuhao.hibernate4.demo.uril;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
Configuration cfg = new Configuration();
cfg.configure(); // 重要
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(
cfg.getProperties()).buildServiceRegistry();
sessionFactory = cfg.buildSessionFactory(sr);
} catch (Exception e) {
e.printStackTrace();
}
}
private HibernateUtil() {
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
hibernate.xml
<?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>
<!-- <property name="hibernate.bytecode.use_reflection_optimizer"></property> -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/forum</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="connection.pool_size">10</property>
<mapping class="com.liuhao.hibernate4.demo.entity.ABCUser"></mapping>
</session-factory>
</hibernate-configuration>
JUnit测试类
package com.liuhao.hibernate4.demo.service.test;
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.jboss.logging.Logger;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import com.liuhao.hibernate4.demo.entity.ABCUser;
import com.liuhao.hibernate4.demo.service.UserService;
import com.liuhao.hibernate4.demo.uril.HibernateUtil;
public class UserServiceTest {
private static Logger log = Logger.getLogger(UserService.class);
@After
public void tearDown() throws Exception {
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void test() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
try {
Transaction tx = (Transaction) session.beginTransaction();
ABCUser user = new ABCUser();
user.setId(8);
user.setName("test");
user.setPwd("liuhao");
session.save(user);
log.debug(user);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
sessionFactory.close();
}
System.out.println("end");
}
}