最近心血来潮,想玩玩jpa(hibernate),ide是(myeclipse5.5),database是(mysql),不知道是怎么回事,一个很简单的实体对象,就id,name两个属性,持久化的时候,真是慢啊,差不过十秒才运行完,运行正常结果,真是让人着急,以前玩hibernate的时候就没有出现这种情况,小弟是一菜鸟,请各位指点...

解决方案 »

  1.   

    实体类
    package com.felix.jpa;import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;@Entity
    public class User {

    private Integer id;

    private String username; @Id @GeneratedValue
    public Integer getId() {
    return id;
    } public void setId(Integer id) {
    this.id = id;
    } public String getUsername() {
    return username;
    } public void setUsername(String username) {
    this.username = username;
    }
    }
    测试类
    package com.felix.jpa;import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;import junit.framework.TestCase;public class JpaBeginTest extends TestCase {


    public void testSave() {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpaService");
    EntityManager manager = factory.createEntityManager();
    manager.getTransaction().begin();

    User user = new User();
    user.setUsername("张三");

    manager.persist(user);

    manager.getTransaction().commit();
    }
    }
      

  2.   

    配置文件
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence" version="1.0">
      <persistence-unit name="jpaService" transaction-type="RESOURCE_LOCAL">
        <properties>
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
          <property name="hibernate.hbm2ddl.auto" value="update"/>
          <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
          <property name="hibernate.connection.username" value="root"/>
          <property name="hibernate.connection.password" value="root"/>
          <property name="hibernate.connection.url" value="jdbc:mysql://localhost/jdbc"/>
        </properties>
      </persistence-unit>
    </persistence>
      

  3.   

    lz用的是什么版本的hibernate。
    怎么越看越与我用的不同:虽然核心文件可以是属性文件或xml。但我用的隶属
     import org.hibernate.*;
    与你的
     import javax.persistence.*;截然不同
    唉,我知识缺乏吧真的不同了
      

  4.   

    创建Factory 速度慢,jpa不是你想象的那个速度的,
    hibernate的Factory也是很慢的
    另外jpa是标准
    hibernate对其实现了,估计hibernate不会重写一套吧,所以你测试的时候实际上底层工作的大部分代码和hiberate是一样的。http://java.sun.com/javaee/technologies/persistence.jsp
      

  5.   


    正如我上面所说 
    hibernate实现了Java Persistence API
    API都是接口 interface
    他import 了接口 而你可能import 了imlp类
      

  6.   

    你们的意思我也懂,可是我就是想跑起来我的第一个jpa,咋就这么难呢
      

  7.   

    我初玩jpa,看着传智播客黎活明老师讲的入门教程来的,我就纳闷了,一样的程序他运行着就挺快的,可是我运行就太慢了...
      

  8.   

    现在用着JPA,倒是看好 IBATIS了。。