How to prevent OpenJPA from updating immutable entities?

4 Messages Forum Options Options
Embed this topic
Permalink
egoosen
How to prevent OpenJPA from updating immutable entities?
Reply Threaded More
Print post
Permalink
I'm using OpenJPA with GWT, and therefore I'm copying my model object to a GWT friendly DTO, before passing it to the frontend (and visa versa when returning from the frontend).

When I check my debug output, I notice that there are update statements for entities that are supposed to be immutable (the database values don't change).
So I tried removing cascade = { CascadeType.PERSIST,CascadeType.MERGE }, to prevent the immutable entity from getting updated, but then I get this error:

org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged object in persistent field

Then I added this method to my DAO:
public TblScmpdt attachImmutableRelationships(TblScmpdt tblScmpdt){
                EntityManager em = getEntityManager();
                TblPdtcde tblPdtcde = em.find(TblPdtcde.class,tblScmpdt.getPdtcdeId());
                tblScmpdt.setTblPdtcde(tblPdtcde);
                return tblScmpdt;
        }

but I still get the InvalidStateException.

How do I stop OpenJPA from trying to merge/persist my immutable entities?
egoosen
Re: How to prevent OpenJPA from updating immutable entities?
Reply Threaded More
Print post
Permalink
I've found a solution to my problem...

tblScmpdt.setTblPdtcde(null); //immutable, don't merge
The foreign key is still set (tblScmpdt.pdtcdeId), so by setting the related entity to null, it prevents OpenJPA from trying to merge it.
timfanelli
Re: How to prevent OpenJPA from updating immutable entities?
Reply Threaded More
Print post
Permalink
I'd like to see the code for your TblPdtcde entity. I suspect that you
could simply set the "updateable=false" property on your column
attribute (or element, if xml) to achieve the same results without
having to impact your object model in this manner.

-Tim

On Wed, 2008-08-13 at 06:36 -0700, egoosen wrote:
> I've found a solution to my problem...
>
> tblScmpdt.setTblPdtcde(null); //immutable, don't merge
> The foreign key is still set (tblScmpdt.pdtcdeId), so by setting the related
> entity to null, it prevents OpenJPA from trying to merge it.


signature.asc (196 bytes) Download Attachment
egoosen
Re: How to prevent OpenJPA from updating immutable entities?
Reply Threaded More
Print post
Permalink
Hi Tim,

I set the "updateable=false" property, but that didn't have any effect.

Here it is:
// Mapped Fields
        @TableGenerator(name="tblPdtcdeGenerator",schema="EBSTATUS",table="TBL_KEYGEN",pkColumnName="PRIMARY_KEY_COLUMN"
                ,valueColumnName="LAST_USED_ID",pkColumnValue="TBL_PDTCDE_ID",allocationSize=1)
                @Id
                @GeneratedValue(strategy=GenerationType.TABLE,generator="tblPdtcdeGenerator")
                @Column(name = "PDTCDE_ID",nullable=false)
                private Integer pdtcdeId;    

        @Basic
        @Column(name = "PDTTYP_CDE",nullable=false,updatable=false)
        private String pdttypCde;

        @Basic
        @Column(name = "PDT_CDE",nullable=false,updatable=false)
        private String pdtCde;

        @Version
        @Column(name = "VRS_NBR")
        private Integer vrsNbr;


// Mapped Relationships
        @OneToMany(mappedBy="tblPdtcde",fetch = FetchType.LAZY)
        private Collection<TblPdtbnfcde> tblPdtbnfcdes = new ArrayList<TblPdtbnfcde>();

        @OneToMany(mappedBy="tblPdtcde",fetch = FetchType.LAZY)
        private Collection<TblScmpdt> tblScmpdts = new ArrayList<TblScmpdt>();