2011-11-08 24 views
5

我在JSF 2项目中使用JPA 2,所以我在GlassFish v3中配置我的数据源,一切正常,但如果我尝试在Hibernate工具中测试JPA查询,它会给出我跟随误差:JPA:找不到TransactionManager

Sessionfactory error:Could not locate TransactionManager 

这里是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" 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_2_0.xsd"> 
    <persistence-unit name="SuaParte" transaction-type="JTA"> 
     <jta-data-source>jdbc/suaparte_ds</jta-data-source> 
     <class>entity.Area</class> 
     <properties> 
      <property name="eclipselink.logging.level" value="FINE"/> 
      <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/> 
     </properties> 


    </persistence-unit> 
</persistence> 

我使用的EclipseLink 2.3(蓝色)和JPA 2在我的Eclipse JSF项目2。

编辑 按照@fonini办法,我hibernate.properties:

hibernate.connection.username=<filled> 
hibernate.connection.password=<filled> 
hibernate.connection.driver_class=com.mysql.jdbc.Driver 
hibernate.dialect=org.hibernate.dialect.MySQLDialect 
hibernate.connection.url=<filled> 
hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider 
hibernate.datasource=jdbc/suaparte_ds 
hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup 

但现在给了我另一个错误: error

难道我不得不改变我的实体id generator

@Entity 
    @Table(name="product") 
    public class Product implements Serializable { 
     private static final long serialVersionUID = 1L; 

     @Id 
     @GeneratedValue(strategy=GenerationType.SEQUENCE) 
     private Integer id; 

     @Size(max=150, message="150 caracteres no máximo") 
     private String description; 

     // getters and setter 
} 
+0

如果您部署项目到GlassFish服务器,它的工作原理? – fonini

+0

它做@fonini,我只是为了测试JPQL查询,发现Hibernate Tools能够做到这一点。 –

+0

MySQL没有序列,它与自动增量一起工作。您必须将GenerationType.SEQUENCE更改为GenerationType.IDENTITY – fonini

回答

0

由于Hibernate Tools在Eclipse内部运行,因此它无法访问特定于应用程序服务器的数据源。

您可以使用属性文件来设置将覆盖数据源的连接。

下面是一个例子文件:

hibernate.connection.username=user 
hibernate.connection.password=pass 
hibernate.connection.driver_class=org.PostgreSQL.Driver 
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect 
hibernate.connection.url=jdbc:postgresql... 

hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider 
hibernate.datasource= 
hibernate.transaction.manager_lookup_class= 
+0

如果在MySQL中会留下什么?我正在使用MySQL,对不起之前没有提及=) –

+0

我可以设置我的hibernate.properties,我更新我的帖子。 –

+1

方言:org.hibernate.dialect.MySQLDialect。如果你使用InnoDB(org.hibernate.dialect.MySQLInnoDBDialect),如果你使用的是MyISAM(org.hibernate.dialect.MySQLMyISAMDialect)。驱动程序类:com.mysql.jdbc.Driver。 URL:jdbc:mysql:// localhost/your_db。希望这可以帮助! – fonini