在使用 Maven 构建期间不会生成表。我的配置中缺少什么?
During build with maven tables are not generated. What am I missing in my configuration?
在 Maven 构建期间,应用程序已成功部署,但 table 未根据应用程序中带有 @entity 注释的模型在数据库中创建。
我得到了一个多模块 maven 项目、Postgres 数据库和 Wildfly 16.0.0.Final。在 Wildfly 中,我创建了数据源并将其与数据库连接。我测试了一个连接并且它有效。在 Maven 构建期间,应用程序已正确部署且没有任何错误,但未创建 tables。我试图使用不同的方言,更改 hibernate.hbm2ddl.auto 值,将带有 @entity 注释的 classes 更改为尽可能基本,以查看错误是否存在于某处但仍然没有 tables 已创建且未显示任何错误。
Persistence.xml:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.2"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<!-- Define persistence unit -->
<persistence-unit name="event-pu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>java:/eventDS</non-jta-data-source>
<class>com.event.model.baseEntity</class>
<class>com.event.model.Event</class>
<class>com.event.model.Group</class>
<class>com.event.model.User</class>
<properties>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/jpa/eventEMF"/>
</properties>
</persistence-unit>
</persistence>
pom.xml 存储模型的模块,我试图为其生成 tables:
<artifactId>Models</artifactId>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.2.13.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.13.Final</version>
<scope>provided</scope>
</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.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
我想在数据库中生成 table 的模型示例:
@Entity
public class Group extends baseEntity {
String name;
String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY)
protected User User;
}
base class 为我的每个模型提供 ID 和时间戳:
@MappedSuperclass
@AttributeOverrides(value = @AttributeOverride(name = "id", column = @Column(columnDefinition = "uniqueidentifier")))
public class baseEntity {
@Id
@GeneratedValue
UUID ID;
@Version
int version;
public UUID getID() {
return ID;
}
public void setID(UUID ID) {
this.ID = ID;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
@Column(columnDefinition = "DATETIME DEFAULT GETDATE()")
private LocalDateTime timestamp = LocalDateTime.now();
public baseEntity() {
}
public LocalDateTime getTimestamp() {
return timestamp;
}
public void setTimestamp(LocalDateTime timestamp) {
this.timestamp = timestamp;
}
@PrePersist
@PreUpdate
public void updateTimestamp() {
timestamp = LocalDateTime.now();
}
}
我想要实现的是在 Maven 构建期间根据使用的注释在数据库中生成所有 table。我的 configuration/code 有什么错误吗?还是我遗漏了什么?
最后,似乎应用程序没有正确部署。我不得不将部署类型从 .jar 更改为 .ear。之后,spring 安全问题已解决。现在应用程序正确部署并创建表。特别感谢 user158037,因为他关于部署类型的问题让我找到了解决方案。
在 Maven 构建期间,应用程序已成功部署,但 table 未根据应用程序中带有 @entity 注释的模型在数据库中创建。
我得到了一个多模块 maven 项目、Postgres 数据库和 Wildfly 16.0.0.Final。在 Wildfly 中,我创建了数据源并将其与数据库连接。我测试了一个连接并且它有效。在 Maven 构建期间,应用程序已正确部署且没有任何错误,但未创建 tables。我试图使用不同的方言,更改 hibernate.hbm2ddl.auto 值,将带有 @entity 注释的 classes 更改为尽可能基本,以查看错误是否存在于某处但仍然没有 tables 已创建且未显示任何错误。
Persistence.xml:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.2"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<!-- Define persistence unit -->
<persistence-unit name="event-pu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>java:/eventDS</non-jta-data-source>
<class>com.event.model.baseEntity</class>
<class>com.event.model.Event</class>
<class>com.event.model.Group</class>
<class>com.event.model.User</class>
<properties>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/jpa/eventEMF"/>
</properties>
</persistence-unit>
</persistence>
pom.xml 存储模型的模块,我试图为其生成 tables:
<artifactId>Models</artifactId>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.2.13.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.13.Final</version>
<scope>provided</scope>
</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.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
我想在数据库中生成 table 的模型示例:
@Entity
public class Group extends baseEntity {
String name;
String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY)
protected User User;
}
base class 为我的每个模型提供 ID 和时间戳:
@MappedSuperclass
@AttributeOverrides(value = @AttributeOverride(name = "id", column = @Column(columnDefinition = "uniqueidentifier")))
public class baseEntity {
@Id
@GeneratedValue
UUID ID;
@Version
int version;
public UUID getID() {
return ID;
}
public void setID(UUID ID) {
this.ID = ID;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
@Column(columnDefinition = "DATETIME DEFAULT GETDATE()")
private LocalDateTime timestamp = LocalDateTime.now();
public baseEntity() {
}
public LocalDateTime getTimestamp() {
return timestamp;
}
public void setTimestamp(LocalDateTime timestamp) {
this.timestamp = timestamp;
}
@PrePersist
@PreUpdate
public void updateTimestamp() {
timestamp = LocalDateTime.now();
}
}
我想要实现的是在 Maven 构建期间根据使用的注释在数据库中生成所有 table。我的 configuration/code 有什么错误吗?还是我遗漏了什么?
最后,似乎应用程序没有正确部署。我不得不将部署类型从 .jar 更改为 .ear。之后,spring 安全问题已解决。现在应用程序正确部署并创建表。特别感谢 user158037,因为他关于部署类型的问题让我找到了解决方案。