无法运行Java JPA示例?

时间:2014-09-30 13:48:14

标签: java mysql jpa transactions wildfly

(旧消息): 我正在尝试按照JPA Reference Guide运行我的Java JPA WAR-Project。我正在使用Wildfly 8和MySQL数据库。但是一旦我尝试使用EntityManager,我就会收到错误。我真的被困在这里了! :(有人知道什么是错的吗?

(编辑): 好的,我现在的状态如下。我无法删除@ManagedBean,因为我需要从XHTML页面(RichFaces)进行调用。因此,我将所有JPA内容放在一个单独的类DbAccess中。程序运行,但只要按下按钮调用Index.doContentMock()然后链接到DbAccess.doContentMock(),我就会收到下面发布的错误。 怎么了?

(编辑2): 由于某种原因,在注入DbAccess的条件后,EntityManager仍然无法工作。 为什么EntityManager不起作用?

我的XHTML调用者类:

import com.glasses.abc.business.Content;
import java.util.List;
import javax.ejb.Stateful;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@ManagedBean
public class Index {

    @Inject
    DbAccess dba;

    public void doContentMock() {
        System.out.println("XXXXX AUFRUFEN VON Index.doContentMock()");
        dba.doContentMock();
    }
}

我的内容实体类:

@Entity
public class Content implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String contentCode;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getContentCode() {
        return this.contentCode;
    }

    public void setContentCode(String contentCode) {
        this.contentCode = contentCode;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Content)) {
            return false;
        }
        Content other = (Content) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.glasses.abc.business.Content[ id=" + id + ", contentCode='" + contentCode + "' ]";
    }

}

我的新DbAccess课程:

@Stateful
public class DbAccess {
    @PersistenceContext(unitName = "AbcPU", type = PersistenceContextType.EXTENDED) // default type is PersistenceContextType.TRANSACTION
    EntityManager em;

    public void doContentMock() {
        System.out.println("XXXXX 0");
        System.out.println(em);
        System.out.println("XXXXX 1");
        Content content = new Content();
        System.out.println("XXXXX 2");
        content.setId((long) 5);
        System.out.println("XXXXX 3");
        content.setContentCode("Hier ist ein bestimmter Code, der in die Datenbank soll!");
        System.out.println("XXXXX 4");
        System.out.println(content);
        try {
        em.persist(content);
        } catch (Exception ex) {
            System.out.println("XXXXX Could not persist:");
            System.out.println(ex.getMessage());
        }
        System.out.println("XXXXX 5");
    }
}

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
    <persistence-unit name="AbcPU" transaction-type="JTA">
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/abc?zeroDateTimeBehavior=convertToNull"/>
            <property name="javax.persistence.jdbc.password" value="pwd"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
        </properties>
    </persistence-unit>
</persistence>

我的错误:

22:58:17,695 INFO  [stdout] (default task-10) XXXXX 0
22:58:17,695 INFO  [stdout] (default task-10) ExtendedEntityManager [abc.war#AbcPU]
22:58:17,695 INFO  [stdout] (default task-10) XXXXX 1
22:58:17,696 INFO  [stdout] (default task-10) XXXXX 2
22:58:17,696 INFO  [stdout] (default task-10) XXXXX 3
22:58:17,696 INFO  [stdout] (default task-10) XXXXX 4
22:58:17,696 INFO  [stdout] (default task-10) com.glasses.abc.business.Content[ id=5, contentCode='Hier ist ein bestimmter Code, der in die Datenbank soll!' ]
22:58:17,697 INFO  [stdout] (default task-10) XXXXX Could not persist:
22:58:17,697 INFO  [stdout] (default task-10) org.hibernate.PersistentObjectException: detached entity passed to persist: com.glasses.abc.business.Content
22:58:17,698 INFO  [stdout] (default task-10) XXXXX 5
22:58:17,702 INFO  [stdout] (default task-10) XXXXX AUFRUFEN VON getContent(0)

1 个答案:

答案 0 :(得分:0)

在持久化上下文注释中添加另一个属性,如下所示: @PersistenceContext(unitName =“AbcPU”,type = PersistenceContextType.EXTENDED)

然后它可能会起作用。