1

Hibernate で初めて JPA を使用しており、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 クラスは次のようになります。

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

最大値: 999999999999999999999999999

増分: 1

次のテスト コードを使用して、自動生成が機能しているかどうかを判断します。

EntityManager entityManager = entityManagerFactory.createEntityManager();

entityManager.getTransaction().begin();

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

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

その結果、null ID が返されます。誰かが問題が何であるか知っていますか?

4

1 に答える 1

2
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());
于 2013-06-20T13:18:37.400 に答える