首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JBoss7.1.0:无法在子部署中找到名为null的持久性单元

JBoss7.1.0:无法在子部署中找到名为null的持久性单元
EN

Stack Overflow用户
提问于 2016-10-05 17:47:54
回答 1查看 4.6K关注 0票数 0

我有一个项目,里面有三个模块:工厂-ear,工厂-ejb和工厂-web.它以EAR的形式部署到JBoss7中,内部有ejb.jar和web.war。当我试图在我的EJB类中获得EntityManager时

代码语言:javascript
复制
@PersistenceContext(unitName = "manager1")
private EntityManager em;

我搞错了

JBAS011440:在部署"factory.ear“的”工厂-EJB1.0-SNAPSHOT.jar“子部署中找不到一个名为manager1的持久性单元

我的persistence.xml

代码语言:javascript
复制
<?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中。

我读到过,这可能是类加载器的问题。我有相同的JPA API依赖关系,并在ear和web模块中提供了作用域。

代码语言:javascript
复制
  <dependency>
     <groupId>org.hibernate.javax.persistence</groupId>
     <artifactId>hibernate-jpa-2.0-api</artifactId>
     <scope>provided</scope>
  </dependency> 

但我没能移除其中的一个。两者都是构建所必需的。

Body.java

代码语言:javascript
复制
@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

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-07 16:02:02

此问题不是由maven依赖关系引起的。

EAR文件中META目录的内容通常是任何类加载程序都无法访问的。

因此,您不能将persistence.xml文件放在EAR文件的META目录中。将其移到EJB文件的META目录中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39880595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档