【问题标题】:JPA with Hibernate - Sequence not working with XMLJPA with Hibernate - 序列不适用于 XML
【发布时间】:2013-06-20 13:12:37
【问题描述】:

我第一次将 JPA 与 Hibernate 一起使用,我尝试使用我的 Oracle 数据库中的现有序列设置自动 ID 生成。

我的实体映射如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
        version="2.0">

    <entity class="AmmAdapter.DataAccessLayer.Models.AmmSyncTypePriorities">
        <table name="AMM_SYNC_TYPE_PRIORITIES" schema="TESTESTERON" catalog=""/>
        <attributes>
            <id name="id">
                <column name="ID" nullable="false" length="10"/>
                <generated-value strategy="SEQUENCE" generator="MODEL_SEQ"/>
                <sequence-generator name="MODEL_SEQ" sequence-name="AMM_ED_GROUP_TYPES_SEQ"/>
            </id>
            <basic name="priority">
                <column name="PRIORITY" nullable="false" length="10"/>
            </basic>
            <one-to-many name="ammSyncMessageTypesesById" mapped-by="ammSyncTypePrioritiesByPriority"
                         target-entity="AmmAdapter.DataAccessLayer.Models.AmmSyncMessageTypes"/>
        </attributes>
    </entity>
</entity-mappings>

我的 Java 类如下所示: 包 AmmAdapter.DataAccessLayer.Models;

import java.util.Collection;

public class AmmEdGroupTypes
{
    private Integer id;

    public Integer getId()
    {
        return id;
    }

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

    private String name;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AmmEdGroupTypes that = (AmmEdGroupTypes)o;

        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;

        return true;
    }

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

    private Collection<AmmEndDeviceGroups> ammEndDeviceGroupsesById;

    public Collection<AmmEndDeviceGroups> getAmmEndDeviceGroupsesById()
    {
        return ammEndDeviceGroupsesById;
    }

    public void setAmmEndDeviceGroupsesById(Collection<AmmEndDeviceGroups> ammEndDeviceGroupsesById)
    {
        this.ammEndDeviceGroupsesById = ammEndDeviceGroupsesById;
    }
}

而我的序列 AMM_ED_GROUP_TYPES_SEQ 有:

最小值:1

最大值:9999999999999999999999999999

增量:1

我使用以下测试代码来确定自动生成是否有效: EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("AdapterPersistence");

EntityManager entityManager = entityManagerFactory.createEntityManager();

entityManager.getTransaction().begin();

AmmEdGroupTypes newGroup = new AmmEdGroupTypes();
newGroup.setName("test");

System.out.println(newGroup.getId());

结果我得到一个空 ID。有人知道问题是什么吗?

【问题讨论】:

  • 持久化对象时会生成Id。

标签: java xml oracle hibernate jpa


【解决方案1】:
EntityManager entityManager = entityManagerFactory.createEntityManager();

entityManager.getTransaction().begin();

AmmEdGroupTypes newGroup = new AmmEdGroupTypes();
newGroup.setName("test");
entityManager.persist(newGroup);
entityManager.getTransaction().commit();
System.out.println(newGroup.getId());

【讨论】:

  • 感谢您的回答,但在发布此问题之前我已经尝试过了。我在您的代码上得到的是以下错误:必须在调用 save() 之前手动分配此类的 ID 你知道我该如何解决这个问题吗? :)
  • 您的 XML 定义了 &lt;entity class="AmmAdapter.DataAccessLayer.Models.AmmSyncTypePriorities"&gt; 的实体,而您正在测试的类是 AmmEdGroupTypes。你有AmmEdGroupTypes 的实体定义吗?
  • 已接受答案。我很乐意投票,但似乎我缺乏这方面的声誉。 ://
猜你喜欢
  • 2017-04-11
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 2015-07-20
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多