Search code examples
jpaopenjpa

Does OpenJPA work with SQL Server IDENTITY?


I'm using SQL Server 2008, OpenJPA 2.2

The persistence setup is OK as findAll() works.

I've tried:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id = 0;

and to persist

public void persist()
{
    EntityManager em = entityManager();
    try
    {
        em.persist(this);
    }
    catch (Exception ex)
    {
        System.out.println(ex);
    }
    finally
    {
        em.close(); 
    }
}

The result is no exception but no data inserted.


Solution

  • Perhaps you need a transaction to commit the data?

    try
        {
            em.getTransaction().begin();
            em.persist(this);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        ...