已分离的实体传递给h2数据库

时间:2017-12-30 13:58:49

标签: jpa h2

我试图将JPA与h2数据库结合使用来构建管理应用程序。

现在我正在尝试在数据库中保存联系人,但在尝试合并新联系人时我总是遇到此错误。

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: ccc.foo.Conact

以下是我现在所拥有的:

班级联系

@SuppressWarnings("serial")
@Entity
@Table
public class Contact implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String firstName;
    private String lastName;
    private String email

    public Contact(String firstName, String lastName, String email{
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.emailAddress = email
    }

    //gets and sets and to string

启动数据库并填写一些联系人的类(现在硬编码):

@SuppressWarnings("serial")
public class InitiateContacts{

private static final String[] contactsFirstNameData = new String[] { "Foo", "Foo2" };
private final String[] contactsLastNameData = new String[] { "FooLastName",  "Foo2LastName" };
private final String[] contactsEmailData = new String[] { "aa@bb.com", "bb@aa.com" };

private EntityManagerFactory emfactory = null;
private EntityManager entitymanager = null;

public InitiateContacts() {
    this.emfactory = Persistence.createEntityManagerFactory("DATABASE_NAME");
    this.entitymanager = emfactory.createEntityManager();
    initPersistentContacts();
}

private void initPersistentContacts() {
    Query query = entitymanager.createQuery("Select COUNT(c) from Contact c");
    Long result = (Long) query.getSingleResult();

    if (result == 0) {
        this.entitymanager.getTransaction().begin();

        for (int i = 0; i < contactsFirstNameData.length && i < contactsLastNameData.length
                && i < contactsEmailData.length; ++i) {
            Contact contact = new Contact(contactsFirstNameData[i], contactsLastNameData[i], contactsEmailData[i]);
            this.entitymanager.merge(contact);   //ERROR HERE!
        }
        this.entitymanager.getTransaction().commit();
    }
}

正如我之前所说,我在行中得到错误:this.entitymanager.merge(contact)。在调试模式中,我看到异常只在第二个循环中引发(我看到了参数,一切都很好)。我认为问题可能是id,但我将其更改为自动生成并且问题仍然存在。

我看到了其他帖子,这里有一些其他用户提供的解决方案对我不起作用:

  • CascadeType.MERGE - 我不需要这个,因为我没有使用与另一个类的多重关系(OneToMany,ManyToMany等)
  • 使用合并而不是保持无差异
  • 使用自动生成的ID

到现在为止我没有更多的想法

可能有用的东西

的persistence.xml

<persistence-unit name="DATABASE_NAME" transaction-type="RESOURCE_LOCAL">
        <!--  <provider>org.hibernate.ejb.HibernatePersistence</provider>-->
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>


        <class>foo.Contact</class>

        <properties>

            <property name="javax.persistence.jdbc.url"
                value="jdbc:h2:C:/path/to/folder" />


            <property name="javax.persistence.jdbc.password" value="qwerty" />
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
            <property name="javax.persistence.jdbc.user" value="admin" />
            <property name="javax.persistence.schema-generation.database.action"
                value="create" />
            <property name="eclipselink.logging.level" value="FINE" />
            <property name="eclipselink.logging.parameters" value="true" />
        </properties>

    </persistence-unit>

ps:如果有相关的话,我可以提供其他的东西

0 个答案:

没有答案