users@glassfish.java.net

Re: JPA ManyToOne howto reference ID?

From: <glassfish_at_javadesktop.org>
Date: Mon, 08 Sep 2008 08:58:59 PDT

Hello,

JPA forces the ManyToOne mapping to be a relationship, so mapping it to a string type isn't going to work. What you can do is use the "parent" field in both a basic mapping and a ManyToOne mapping. ie:

    @ManyToOne( fetch=LAZY )
    @JoinColumn(name = "parent")
    protected MyObject parent;

    @Basic
    @Column(name= "parent", insertable=false, updatable=false)
    protected string parentId;

This will allow you to use the relationship when needed, and to set/insert the field using the relationship. But when you just need the id, you can access it using the parentId. Because the parent mapping is lazy loaded, it will only be populated/read from the database when it is accessed.

Hope this helps.

Chris
[Message sent by forum member 'chris_delahunt' (chris_delahunt)]

http://forums.java.net/jive/thread.jspa?messageID=297841