Wednesday, February 27, 2013

Eclipselink Postgres GeneratedValue strategy=Identity not compatible with HistoryPolicy

I wanted to do this in my Eclipselink/Postgres application:
       
        TABLE
    -------------------
    ID SERIAL NOT NULL

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column

    private long id;

This produces exceptions upon persist().   I think it's this bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=347539

 Solution is to do this

        TABLE
    -------------------
    ID SERIAL NOT NULL

 It's important to know that SERIAL implicitly creates a sequence named TABLE_ID_SEQ.  So I can use strategy=SEQUENCE to use it:

    @Id
    @SequenceGenerator(name="table_id_seq", allocationSize=1)
    @GeneratedValue(strategy=SEQUENCE, generator="table_id_seq")
    @Column
    private long id;


It's important to include allocationSize=1, otherwise Eclipselink will start using nextval() - 50 as the next ID... producing Primary Key conflicts upon persist.

 

JPA Eclipselink ManyToOneMapping does not support HistoryPolicy

I tried creating an Entity with a nullable @ManyToOne relationship using a @JoinTable.  While JPA supports this just fine, I ran into problems when trying to Implement a EclipseLink's HistoryPolicy using an EntityCustomizer. 

I wanted to do this:

ManyToOneMapping mapping = (ManyToOneMapping)descriptor.getMappingForAttributeName(FIELD_NAME);
mapping.setHistoryPolicy(...);

Alas, ManyToOneMapping has no setHistoryPolicy() method....it just isn't an implemented feature.

I searched the Web and found little help.  Documentation for HistoryPolicy is very light.  I did find some suggestions that HistoryPolicy only supports some mappings but not others.

Conclusion: ManyToOne relationships with a JoinTable are not compatible with HistoryPolicy.  My workaround is to use a nullable JoinColumn for this relationship.




Vaadin JPAContainer JPAContainerItem.setWriteThrough(false) OptimisticLockException

I was getting an OptimisticLockException (OPE) while using Vaadin JPAContainer setWriteThrough(false)

item.setWriteThrough(false);
item.getItemProperty(...).setValue(...);

item.getItemProperty(...).setValue(...);
item.getItemProperty(...).setValue(...);
item.setWriteThrough(true);

But I did not get this error if I setwriteThrough true.

Turns out that the first case calls MutableLocalEntityProvider.updateEntity() while the second case called MutableLocalEntityProvider.updateEntityProperty(). 

JpaContainerItem holds a reference to an entity that is usually (but not always) detached. 

updateEntity() simply merges this entity to the Persistence Context and commits.  If the version doesn't match the database, the OPE is thrown.  

 updateEntityProperty fetches a fresh entity with em.find(), refreshes it, sets the new value, commits and detaches the entity.  Only a concurrent write to the database would cause a OPE.

Under both scenarios, setValue() does not cause the Item's referred entity to be refreshedthe version number does not  change.  Basically, OPE is thrown when updateEntity() called after any changes have been committed.

 

 


Friday, February 22, 2013

Transfer Windows XP Installation CD to USB

I needed to re-install Windows XP on a Netbook before selling it.  I transferred the Windows Installation CD to a USB stick using WinToFlash.  Worked fine.  A little worried about spyware though.  The installer tried to install a Firefox plug-in.

http://wintoflash.com/home/en/

-M

Thursday, February 21, 2013

Wiping Hard Drives

I wanted to wipe a hard drive on my old netbook before selling it.  I had success by creating a bootable USB stick with DBAN - Darik's Boot and Nuke.

Pen Drive Linux :  Installs a OS image (iso) to a USB stick and makes it bootable
http://www.pendrivelinux.com/universal-usb-installer-easy-as-1-2-3/

DBAN :  a small Linux Distro for Nuking Hard disks.
http://sourceforge.net/projects/dban/

DBAN 'autonuke' wipes all drives on the system, including the DBAN USB stick, would be nice to know how to only wipe specified drives....maybe next time.

This looks like a useful tool to have in the box:
http://www.ultimatebootcd.com/