[英]What is the configuration to use for the combination MySQL-Server/Apache Tomcat and a Tapestry project using Hibernate/JPA?
I have a MySQL Server running on an Apache Tomcat. 我有一个在Apache Tomcat上运行的MySQL服务器。
I also have created a Tapestry project via Quickstart Archetype. 我还通过Quickstart Archetype创建了Tapestry项目。 I have then added dependencies to tapestry-hibernate and and the mysql connector in the POM. 然后,我将依赖项添加到了tapestry-hibernate和POM中的mysql连接器。
<dependency>
<groupId>org.apache.tapestry</groupId>
<artifactId>tapestry-hibernate</artifactId>
<version>${tapestry-release-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
I have created an entity with the appropriate annotations (Entity, Id, Table, Column, etc.). 我创建了带有适当注释(实体,Id,表,列等)的实体。
I do not want to use the Hibernate API, but the Hibernate JPA "subset". 我不想使用Hibernate API,而是使用Hibernate JPA“子集”。
I have a persistence.xml in the java/main/resources/META-INF folder and a hibernate.cfg.xml in the java/main/resources folder. 我在java / main / resources / META-INF文件夹中有一个persistence.xml,在java / main / resources文件夹中有一个hibernate.cfg.xml。
The persistence.xml: persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="2.0">
<persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value = "true" />
<property name="hibernate.format_sql" value = "true" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/someDB" />
<property name="javax.persistence.jdbc.user" value="someUser" />
<property name="javax.persistence.jdbc.password" value="somePassword" />
</properties>
</persistence-unit>
</persistence>
My hibernate.cfg.xml: 我的hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- For use of Hibernate with a MySQL-DB over a webserver -->
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/someDB"</property>
<property name="hibernate.connection.username">someUser</property>
<property name="hibernate.connection.password">somePassword</property>
<!-- List of XML mapping files -->
</session-factory>
</hibernate-configuration>
I started the tapestry application using jetty:run. 我使用jetty:run启动了挂毯应用程序。
The problem: The app starts, I can view the index page, but the database isn't accessed or modified. 问题:应用程序启动,我可以查看索引页面,但是无法访问或修改数据库。
I want the database and tables to be created automatically. 我想要自动创建数据库和表。 I know that hibernate.hbm2ddl.auto=create creates the tables. 我知道hibernate.hbm2ddl.auto = create创建表。 Does it also create the database or does it have to be created manually? 它还会创建数据库还是必须手动创建?
I only have one entity and the basic tapestry pages in my code. 我的代码中只有一个实体和基本的挂毯页面。 Do I explicitly have to at least define an EntityManager to make the program create the tables? 我是否必须至少明确定义EntityManager才能使程序创建表?
Do I have to have both the persistence.xml and the hibernate.cfg.xml? 我必须同时拥有persistence.xml和hibernate.cfg.xml吗? It seems some properties are redundant... 似乎有些属性是多余的...
If I want to use Hibernate just as a JPA implementation and not the hibernate-specific API, are the properties in the configuration files the correct ones? 如果我只想将Hibernate用作JPA实现,而不要使用特定于Hibernate的API,那么配置文件中的属性是否正确?
First of all add the dependencies in the pom for the mysql - 首先在mysql的pom中添加依赖项-
<dependency>
<groupId>org.apache.tapestry</groupId>
<artifactId>tapestry-hibernate</artifactId>
<version>${tapestry-release-version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hibernate-tapestry-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-version}</version>
</dependency>
and modify your hibernate.cfg.xml file with the following configuration 并使用以下配置修改您的hibernate.cfg.xml文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tapestry_db?createDatabaseIfNotExist=true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
This is working for me. 这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.