gpt4 book ai didi

hibernate - WildFly + hibernate

转载 作者:行者123 更新时间:2023-12-02 23:38:36 25 4
gpt4 key购买 nike

我几天来一直在努力配置 Hibernate 并在 WildFly 上运行它。

这是我的代码:

META-INF/persistence.xml

<persistence version="1.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_1_0.xsd">

<persistence-unit name="blog" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/blog?createDatabaseIfNotExist=true"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="abc"/>
<property name="hibernate.connection.password" value="abc"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
<property name="jboss.as.jpa.providerModule" value="org.hibernate:5.0"/>
</properties>

</persistence-unit>

pom.xml

<dependencies>

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>

<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.2.Final</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

</dependencies>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

用户:

@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name = "username")
private String username;

@Column(name = "password")
private String password;

@OneToMany(mappedBy = "user")
private Set<Post> posts;
}

当我加载主页时,没有创建数据库。

此外,我想保留一个用户。

@Stateless
public class UserRepositoryImpl implements UserRepository {

@PersistenceContext(unitName = "blog")
private EntityManager entityManager;

public void create(User user) {
this.entityManager.persist(user);
}
}

未创建数据库且实体管理器为空。我需要配置什么才能让它运行?我正在使用 IntelliJ 进行测试。

最佳答案

JTA 数据源由 jpa 容器(在 Wildfly 内部)管理。您必须在standalone.xml 中定义url、用户名、密码。搜索数据源子系统 <subsystem xmlns="urn:jboss:domain:datasources:4.0">并添加数据源定义,例如:

<datasource jta="true" jndi-name="java:/jdbc/myDS" pool-name="MyDS" enabled="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:mysql://localhost:3306/blog</connection-url>
<driver>mysqldriver.jar</driver>
<security>
<user-name>username</user-name>
<password>password</password>
</security>
</datasource>

接下来您必须为数据库驱动程序创建一个模块。有关详细信息,请查看文档:https://docs.jboss.org/author/display/WFLY8/DataSource+configuration

然后你的 persistence.xml 将如下所示:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">

<persistence-unit name="blog">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc/myDS</jta-data-source>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>

在服务器和组件启动期间监视异常。

关于hibernate - WildFly + hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41466824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com