1

I'm using JPA with Hibernate for the first time and I try to set up automated ID generation using existing sequences from my Oracle database.

My entity mapping looks as follows:

<?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>

My Java class looks as follows: package 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;
    }
}

And my sequence AMM_ED_GROUP_TYPES_SEQ has:

Min value: 1

Max value: 9999999999999999999999999999

Increment by: 1

I use the following test code to determine if auto generation is working: EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("AdapterPersistence");

EntityManager entityManager = entityManagerFactory.createEntityManager();

entityManager.getTransaction().begin();

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

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

And I get a null ID as result. Does someone know what the problem is?

1
  • Id will be generated when you persist the object. Commented Jun 20, 2013 at 13:17

1 Answer 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());
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, but I've tried that before posting this question. What I get on your code is the following error: ids for this class must be manually assigned before calling save() Do you know how I can fix this? :)
Your XML defines the entity for <entity class="AmmAdapter.DataAccessLayer.Models.AmmSyncTypePriorities"> whereas the class with which you are testing is AmmEdGroupTypes. Do you have the entity definition for AmmEdGroupTypes?
Answer accepted. I would gladly upvote but it seems I lack the reputation for that. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.