我在Eclipse中创建了一个JSF EJB JPA应用程序,现在我想使用Hibernate。我有一个正在运行的MariaDB设置,在我的Standalone.xml中添加了一个数据源,并为MySQL创建了一个module.xml。
在我的上一个项目中(我自己没有设置)我过去只是简单地创建带注释的实体,表格会自动创建,我不需要编写任何映射。我该如何设置?例如:
@Entity
@Table(name = "user")
public class User extends BaseEntity {
private static final long serialVersionUID = 1L;
@NotNull
@Size(min = 4, max = 50)
@Column(name = "username", unique = true, nullable = false)
private String username;
@NotNull
@Size(min = 4, max = 255)
@Column(name = "password", nullable = false)
private String password;
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
// ...
}
我阅读了几个教程,但我不确定我是否理解了所有内容。
我以前用这样生成一个EntityManager:
@ApplicationScoped
public class EntityManagerProducer {
@PersistenceUnit(unitName = "primary")
private EntityManagerFactory entityManagerFactory;
@Produces
@Default
@TransactionScoped
protected EntityManager exposeEntityManagerProxy() {
return entityManagerFactory.createEntityManager();
}
protected void onTransactionEnd(@Disposes @Default EntityManager entityManager) {
if (entityManager.isOpen()) {
entityManager.close();
}
}
}
我想使用相同的类--Hibernate如何知道要加载哪个类?这是如何配置的?
UPDATE1:WildFly CLI在添加JDBC驱动程序后返回此信息:
[standalone@localhost:9990 /] /subsystem=datasources:installed-drivers-list
{
"outcome" => "success",
"result" => [{
"driver-name" => "mysql",
"deployment-name" => undefined,
"driver-module-name" => "com.mysql",
"module-slot" => "main",
"driver-datasource-class-name" => "",
"driver-xa-datasource-class-name" => "com.mysql.jdbc.jdbc2.optional.Mysq
lXADataSource",
"driver-class-name" => "com.mysql.jdbc.Driver",
"driver-major-version" => 5,
"driver-minor-version" => 1,
"jdbc-compliant" => false
}]
}
答案 0 :(得分:1)
使用例如
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.7.Final</version>
<scope>provided</scope>
</dependency>
在META-INF文件夹中使用persistence.xml文件。这是一个可以帮助您的片段。 这要求您已在Wildfly中设置myDS数据源。
<persistence-unit name="myDS">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/myDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.default_schema" value="public"/>
<property name="hibernate.max_fetch_depth" value="4"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
<property name="javax.persistence.schema-generation.create-source" value="script"/>
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/sql/create-script.sql"/>
<property name="javax.persistence.schema-generation.drop-source" value="script"/>
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/sql/drop-script.sql"/>
<property name="javax.persistence.sql-load-script-source" value="META-INF/sql/data.sql"/>
<property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="target/jpa/sql/create-schema.sql"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="target/jpa/sql/drop-schema.sql"/> -
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.hbm2ddl.import_files" value="META-INF/sql/data.sql"/>
</properties>
为了自动创建项目实体中的表格,您可以指定删除,创建和插入/更新要执行的sql文件。已经包含在上面的代码段中。阅读每个房产的用途,并使用符合您项目需求的房产。
请参阅here