Showing posts with label Persistence. Show all posts
Showing posts with label Persistence. Show all posts

Thursday, September 15, 2011

Hibernate Search 4.0.0.Beta1 Release

Hibernate Search 4.0.0.Beta1 has been released here.

Hibernate Core 4.0.0.CR3 Release

Hibernate Core 4.0.0.CR3 has been released here.

Thursday, September 8, 2011

Upgrade Hibernate Core 3.6 to 4.0

Be careful with updating Hibernate Core 3.6 to 4.0 with MySQL as database and strategy GenerationType.AUTO. For me it was an update of JBoss AS 6 (Hibernate 3.6) to AS 7 (Hibernate 4). If you have mapped tables like this:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Hibernate 3.6 uses the auto increment functionality to increment the id. But Hibernate 4 creates a sequence table and uses this to increment the id. Even if you say sequences are ok, you will run into exceptions on a given database, since Hibernate tries to save the first object with id 1 which already exists. Better change the strategy to:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Now both Hibernate versions will create the table structure in the same way.

Wednesday, September 7, 2011

Hibernate Core 4.0.0.CR2 Release

Hibernate Core 4.0.0.CR2 has been released here.

Tuesday, September 6, 2011

Accessing database during application initialization

In development state most applications need dummy data which should be imported on application initialization. During this phase Seam's TransactionManager is not initialized yet. This means we have to build our UserTransaction manually:

@PersistenceContext
private EntityManager em;

@Inject
private UserTransaction utx;

public void importData(@Observes @Initialized WebApplication webapp) {
    try {
        utx.begin();
        em.persist(new MyDatabaseEntity());
        utx.commit();
    } catch (Exception e) {
        log.error("Import failed. Seed data will not be available.", e);
        try {
            if (utx.getStatus() == Status.STATUS_ACTIVE) {
                try {
                    utx.rollback();
                } catch (Exception rbe) {
                    log.error("Error rolling back transaction", rbe);
                }
            }
        } catch (Exception se) {
        }
    }
}