JBoss7.1.0: 在子部署中找不到名为 null 的持久性单元
JBoss7.1.0: Can't find a persistence unit named null in subdeployment
我有一个项目,里面有 3 个模块:factory-ear、factory-ejb 和 factory-web。它作为 EAR 部署到 JBoss7,其中包含 ejb.jar 和 web.war。
当我试图在我的 EJB 中获取 EntityManager 时 class
@PersistenceContext(unitName = "manager1")
private EntityManager em;
我收到错误
JBAS011440: Can't find a persistence unit named manager1 in subdeployment
"factory-ejb-1.0-SNAPSHOT.jar" of deployment "factory.ear"
我的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="manager1" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/PostgresDS</jta-data-source>
<class>com.test.model.Car</class>
<class>com.test.model.Engine</class>
<class>com.test.model.Body</class>
<class>com.test.model.Transmission</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
persistence.xml在app_name/factory_ear/META-INF.
我了解到这可能是 classloader 的问题。我在 ear 和 web 模块中具有相同的 JPA API 依赖关系
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<scope>provided</scope>
</dependency>
但我无法删除其中一个。两者都是构建所必需的。
Body.java
@Entity
@Table(name = "body")
public class Body {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name = "type")
private String type;
@Column(name = "color")
private String color;
@Column(name = "doors_num")
private Integer doorsNumber;
@Column(name = "vin")
private Integer vin;
@OneToMany(mappedBy = "body")
private Set<Car> cars;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(String color) {
this.color = color;
}
/**
* @return the doorsNumber
*/
public Integer getDoorsNumber() {
return doorsNumber;
}
/**
* @param doorsNumber the doorsNumber to set
*/
public void setDoorsNumber(Integer doorsNumber) {
this.doorsNumber = doorsNumber;
}
/**
* @return the vin
*/
public Integer getVin() {
return vin;
}
/**
* @param vin the vin to set
*/
public void setVin(Integer vin) {
this.vin = vin;
}
/**
* @return the cars
*/
public Set<Car> getCars() {
return cars;
}
/**
* @param cars the cars to set
*/
public void setCars(Set<Car> cars) {
this.cars = cars;
}
}
Car.java
public class Car {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name = "model")
private String model;
@ManyToOne
@JoinColumn(name = "engine_id")
private Engine engine;
@ManyToOne
@JoinColumn(name = "body_id")
private Body body;
@ManyToOne
@JoinColumn(name = "transmission_id")
private Transmission transmission;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
/**
* @return the engine
*/
public Engine getEngine() {
return engine;
}
/**
* @param engine the engine to set
*/
public void setEngine(Engine engine) {
this.engine = engine;
}
/**
* @return the body
*/
public Body getBody() {
return body;
}
/**
* @param body the body to set
*/
public void setBody(Body body) {
this.body = body;
}
/**
* @return the transmission
*/
public Transmission getTransmission() {
return transmission;
}
/**
* @param transmission the transmission to set
*/
public void setTransmission(Transmission transmission) {
this.transmission = transmission;
}
}
这个问题不是你的maven依赖引起的。
任何 class 加载器通常无法访问 EAR 文件中 META-INF 目录的内容。
因此,您不能将 persistence.xml 文件放在 EAR 文件的 META-INF 目录中。而是将其移动到 EJB jar 文件的 META-INF 目录中。
我有一个项目,里面有 3 个模块:factory-ear、factory-ejb 和 factory-web。它作为 EAR 部署到 JBoss7,其中包含 ejb.jar 和 web.war。 当我试图在我的 EJB 中获取 EntityManager 时 class
@PersistenceContext(unitName = "manager1")
private EntityManager em;
我收到错误
JBAS011440: Can't find a persistence unit named manager1 in subdeployment "factory-ejb-1.0-SNAPSHOT.jar" of deployment "factory.ear"
我的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="manager1" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/PostgresDS</jta-data-source>
<class>com.test.model.Car</class>
<class>com.test.model.Engine</class>
<class>com.test.model.Body</class>
<class>com.test.model.Transmission</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
persistence.xml在app_name/factory_ear/META-INF.
我了解到这可能是 classloader 的问题。我在 ear 和 web 模块中具有相同的 JPA API 依赖关系
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<scope>provided</scope>
</dependency>
但我无法删除其中一个。两者都是构建所必需的。
Body.java
@Entity
@Table(name = "body")
public class Body {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name = "type")
private String type;
@Column(name = "color")
private String color;
@Column(name = "doors_num")
private Integer doorsNumber;
@Column(name = "vin")
private Integer vin;
@OneToMany(mappedBy = "body")
private Set<Car> cars;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(String color) {
this.color = color;
}
/**
* @return the doorsNumber
*/
public Integer getDoorsNumber() {
return doorsNumber;
}
/**
* @param doorsNumber the doorsNumber to set
*/
public void setDoorsNumber(Integer doorsNumber) {
this.doorsNumber = doorsNumber;
}
/**
* @return the vin
*/
public Integer getVin() {
return vin;
}
/**
* @param vin the vin to set
*/
public void setVin(Integer vin) {
this.vin = vin;
}
/**
* @return the cars
*/
public Set<Car> getCars() {
return cars;
}
/**
* @param cars the cars to set
*/
public void setCars(Set<Car> cars) {
this.cars = cars;
}
}
Car.java
public class Car {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name = "model")
private String model;
@ManyToOne
@JoinColumn(name = "engine_id")
private Engine engine;
@ManyToOne
@JoinColumn(name = "body_id")
private Body body;
@ManyToOne
@JoinColumn(name = "transmission_id")
private Transmission transmission;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
/**
* @return the engine
*/
public Engine getEngine() {
return engine;
}
/**
* @param engine the engine to set
*/
public void setEngine(Engine engine) {
this.engine = engine;
}
/**
* @return the body
*/
public Body getBody() {
return body;
}
/**
* @param body the body to set
*/
public void setBody(Body body) {
this.body = body;
}
/**
* @return the transmission
*/
public Transmission getTransmission() {
return transmission;
}
/**
* @param transmission the transmission to set
*/
public void setTransmission(Transmission transmission) {
this.transmission = transmission;
}
}
这个问题不是你的maven依赖引起的。
任何 class 加载器通常无法访问 EAR 文件中 META-INF 目录的内容。
因此,您不能将 persistence.xml 文件放在 EAR 文件的 META-INF 目录中。而是将其移动到 EJB jar 文件的 META-INF 目录中。