对于一个周末项目,我尝试使用 Hibernate 3.5 运行 JPA 2。请注意,我没有收到任何编译错误或运行时异常(当我在 Tomcat 上部署 war 时)。以下是我的代码 -
持久性.xml
<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_2_0.xsd"
version="2.0">
<persistence-unit name="postage" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"></property>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postage"></property>
<property name="hibernate.connection.username" value="postage"></property>
<property name="hibernate.connection.password" value="postage"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"></property>
<property name="hibernate.hbm2ddl.auto" value="create-drop"></property>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"></property>
</properties>
</persistence-unit>
</persistence>
hibernate .cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="current_session_context_class">thread</property>
<!-- mapping files -->
<mapping class="net.rocky.postage.domain.Post"/>
<mapping class="net.rocky.postage.domain.User"/>
</session-factory>
</hibernate-configuration>
我有 2 个简单的实体类 -
@Entity
@Table(name="Post")
public class Post implements Serializable{
@Id
@GeneratedValue
private long id;
private String description;
private String comments;
@OneToOne
private User postedBy;
和
@Entity
@Table(name="PostageUser")
public class User implements Serializable{
@Id
@GeneratedValue
private long id;
private String username;
private String password;
我是不是漏掉了什么。我整个星期六都在尝试所有组合。请帮助我。
跟进 1:我所说的不工作是指 - 当我在 Tomcat 上部署应用程序时,我没有看到在 Postgres 中创建了 2 个表(因为我在 hbm2ddl 中给出了 create-drop)。
跟进2:感谢您的回复。我什至无法让 Hibernate 记录消息。这是我的配置:
log4j.appender.S=org.apache.log4j.ConsoleAppender
log4j.appender.S.layout=org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
log4j.rootCategory=DEBUG, S
此外,我在 maven 中添加了 -
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
仍然没有来自 hibernate 的日志消息
最佳答案
我无法在映射或配置中发现任何明显的错误(尽管在容器中运行时您通常应该使用数据源,但您的配置可以工作)。但这里有一些建议或想法:
- 告诉我们更多关于您应用程序的结构和打包的信息(
persistence.xml
在哪里?)。 - 启用 DEBUG 日志记录:
- 对于类别
org.hibernate.tool.hbm2ddl
- 甚至可能是整个
org.hibernate
类别,直到你让它工作为止
- 对于类别
- 运行 JUnit 测试(容器外),这将使调试更容易。
跟进:我没有看到在您的日志记录配置中定义任何 Hibernate 记录器。您需要为类别 org.hibernate.tool.hbm2ddl
添加以下内容:
log4j.logger.org.hibernate.hbm2ddl=debug
我还想知道您的 persistence.xml
在您的 Maven 项目中的位置。
引用资料
关于java - 无法让 JPA 与 Hibernate 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3486727/