You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by CarpathianAnonymous <an...@yahoo.com> on 2012/04/25 12:20:37 UTC

"element" column of the associated table is null

I have an Interface (IInterface) and 2 classes that implements that interface
(IInterface). Also in another class ( Holder ) I have a collection of
interface items ( Collection<IInterface> ). 

When I try to execute the code from OpenJPA_Test.main, in my associated
table ( holder_classes), the column which is supposed to hold the references
to Class1 or Class2 objects are NULL. 

public class OpenJPA_Test { 

    private static HolderDao holderDao =
EntityDaoFactory.inst().getHolderDao(); 
    
    /** 
     * @param args the command line arguments 
     */ 
    public static void main(String[] args) { 
        // TODO code application logic here 
        Holder h = new Holder(); 
        LinkedList<IInterface> list = new LinkedList<IInterface>(); 
        
        Class1 c1 = new Class1(); 
        Class2 c2 = new Class2(); 
        
        list.add(c1); 
        list.add(c2); 
        
        h.setClasses(list); 
        holderDao.create(h); 
        
    } 
} 


@ManagedInterface 
public interface IInterface { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    int getId(); 
    void setId(int id); 
} 


@Entity 
@Table(name = "class1") 
public class Class1 implements IInterface { 

    @Id 
    private int id; 
    @Basic 
    private int number; 

    public Class1() { 
    } 

    @Override 
    public int getId() { 
        return id; 
    } 

    @Override 
    public void setId(int id) { 
        this.id = id; 
    } 

    public int getNumber() { 
        return number; 
    } 

    public void setNumber(int number) { 
        this.number = number; 
    } 
} 



@Entity 
@Table(name = "class2") 
public class Class2 implements IInterface { 

    @Id 
    private int id; 
    @Basic 
    private String text; 
    
    public Class2(){ 
    } 

    public String getText() { 
        return text; 
    } 

    public void setText(String text) { 
        this.text = text; 
    } 

    public int getId() { 
        return id; 
    } 

    public void setId(int id) { 
        this.id = id; 
    } 
} 



@Entity 
@Table(name = "holder") 
public class Holder { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int id; 
    @OneToMany(cascade = CascadeType.ALL) 
    private Collection<IInterface> classes; 
    
    public Holder(){ 
    } 

    public Collection<IInterface> getClasses() { 
        return classes; 
    } 

    public void setClasses(Collection<IInterface> classes) { 
        this.classes = classes; 
    } 

    public int getId() { 
        return id; 
    } 

    public void setId(int id) { 
        this.id = id; 
    } 
} 

--
View this message in context: http://openjpa.208410.n2.nabble.com/element-column-of-the-associated-table-is-null-tp7498933p7498933.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: "element" column of the associated table is null

Posted by CarpathianAnonymous <an...@yahoo.com>.
Hi Rick,

Thank's a lot for you help.
For the moment I will implement the 2nd solution.

I've also added the bug to JIRA.
https://issues.apache.org/jira/browse/OPENJPA-2181
https://issues.apache.org/jira/browse/OPENJPA-2181 

--
View this message in context: http://openjpa.208410.n2.nabble.com/element-column-of-the-associated-table-is-null-tp7498933p7506209.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: "element" column of the associated table is null

Posted by Rick Curtis <cu...@gmail.com>.
It appears that this in an OpenJPA bug. Could I have you open a JIRA for
this issue and attach your testcase?

I'm not sure when I'll get time to try to fix this issue, but for the short
term you have a couple workaround options....Option #1 -- you could change
your ID generation strategy to AUTO[1]. Option #2 -- don't rely on the
cascade persist functionality. Manually persist each of your children
objects (Class1, Class2) before persisting Holder[2].

Thanks,
Rick

[1]
@Id
@GeneratedValue(strategy = GenerationType.AUTO)

[2]
class HolderDao ....
    public boolean create(Holder h) {
        EntityTransaction trans = em.getTransaction();

        trans.begin();

        for (IInterface i : h.getClasses()) {
            em.persist(i);
        }
        em.persist(h);
        trans.commit();

        return true;
    }

On Thu, Apr 26, 2012 at 10:14 AM, CarpathianAnonymous <
andreibratu2004@yahoo.com> wrote:

> I've attached my UnitTest
>
> http://openjpa.208410.n2.nabble.com/file/n7503208/src.rar src.rar
>
> --
>



-- 
*Rick Curtis*

Re: "element" column of the associated table is null

Posted by CarpathianAnonymous <an...@yahoo.com>.
I've attached my UnitTest

http://openjpa.208410.n2.nabble.com/file/n7503208/src.rar src.rar 

--
View this message in context: http://openjpa.208410.n2.nabble.com/element-column-of-the-associated-table-is-null-tp7498933p7503208.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: "element" column of the associated table is null

Posted by Rick Curtis <cu...@gmail.com>.
Can you create a small unit test that exhibits this behavior? My test that
I put together yesterday worked as expected.

Thanks,
Rick

On Thu, Apr 26, 2012 at 6:22 AM, CarpathianAnonymous <
andreibratu2004@yahoo.com> wrote:

> Ok, done but with the same result
>
>
-- 
*Rick Curtis*

Re: "element" column of the associated table is null

Posted by CarpathianAnonymous <an...@yahoo.com>.
Ok, done but with the same result

--
View this message in context: http://openjpa.208410.n2.nabble.com/element-column-of-the-associated-table-is-null-tp7498933p7502522.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: "element" column of the associated table is null

Posted by Rick Curtis <cu...@gmail.com>.
Okay, get rid of the @ManagedInterface and put the @Id /
@GeneratedValue(strategy = GenerationType.IDENTITY) in your concrete
classes(Class1, Class2).

Thanks,
Rick

On Wed, Apr 25, 2012 at 11:39 AM, CarpathianAnonymous <
andreibratu2004@yahoo.com> wrote:

> Yes, i've tryied also without @ManagedInterface and got the same result.
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/element-column-of-the-associated-table-is-null-tp7498933p7500049.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
*Rick Curtis*

Re: "element" column of the associated table is null

Posted by CarpathianAnonymous <an...@yahoo.com>.
Yes, i've tryied also without @ManagedInterface and got the same result.

--
View this message in context: http://openjpa.208410.n2.nabble.com/element-column-of-the-associated-table-is-null-tp7498933p7500049.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: "element" column of the associated table is null

Posted by Rick Curtis <cu...@gmail.com>.
Can you try removing @ManagedInterface to see if that helps? If not, I'll
try to recreate.

Thanks,
Rick

On Wed, Apr 25, 2012 at 9:57 AM, CarpathianAnonymous <
andreibratu2004@yahoo.com> wrote:

> Hello Rick,
>
> I'm using version 2.1.1 of OpenJPA but i've tried also with 2.2.0.
> The classes are enhanced using the following code in build.xml file:
>
> <target name="-pre-jar">
>        <echo message="begin openJPAC"/>
>        <path id="openjpa.path.id">
>            <pathelement location="${build.classes.dir}"/>
>
>
>            <fileset
> dir="C:\Users\jan\Documents\NetBeansProjects\[Libs]\Apache_OpenJPA\v2.1.1\"
> includes="*.jar"/>
>
>
>
>        </path>
>
>        <taskdef name="openjpac"
> classname="org.apache.openjpa.ant.PCEnhancerTask">
>            <classpath refid="openjpa.path.id"/>
>        </taskdef>
>
>        <openjpac>
>            <classpath refid="openjpa.path.id"/>
>        </openjpac>
>        <echo message="end openJPAC"/>
>    </target>
>
> The @ManagedInterface annotation is not necessary, I was just testing with
> it.
>
>

-- 
*Rick Curtis*

Re: "element" column of the associated table is null

Posted by CarpathianAnonymous <an...@yahoo.com>.
Hello Rick,

I'm using version 2.1.1 of OpenJPA but i've tried also with 2.2.0.
The classes are enhanced using the following code in build.xml file:

<target name="-pre-jar">        
        <echo message="begin openJPAC"/>
        <path id="openjpa.path.id">        
            <pathelement location="${build.classes.dir}"/>

            
            <fileset
dir="C:\Users\jan\Documents\NetBeansProjects\[Libs]\Apache_OpenJPA\v2.1.1\"
includes="*.jar"/>
             
            
            
        </path>

        <taskdef name="openjpac"
classname="org.apache.openjpa.ant.PCEnhancerTask">
            <classpath refid="openjpa.path.id"/>
        </taskdef>

        <openjpac>            
            <classpath refid="openjpa.path.id"/>
        </openjpac>
        <echo message="end openJPAC"/>
    </target>

The @ManagedInterface annotation is not necessary, I was just testing with
it.

--
View this message in context: http://openjpa.208410.n2.nabble.com/element-column-of-the-associated-table-is-null-tp7498933p7499646.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: "element" column of the associated table is null

Posted by Rick Curtis <cu...@gmail.com>.
What version of OpenJPA are you using and how are you enhancing your
entities?

Also, is there are particular reason that you have the @ManagedInterface
annotation on your IInterface interface? Unless you plan on using some of
the documented managed interface[1] support, *I think* you can safely
remove that.

Thanks,
Rick

[1]
http://openjpa.apache.org/builds/latest/docs/docbook/manual.html#ref_guide_pc_interfaces

On Wed, Apr 25, 2012 at 5:20 AM, CarpathianAnonymous <
andreibratu2004@yahoo.com> wrote:

> I have an Interface (IInterface) and 2 classes that implements that
> interface
> (IInterface). Also in another class ( Holder ) I have a collection of
> interface items ( Collection<IInterface> ).
>
> When I try to execute the code from OpenJPA_Test.main, in my associated
> table ( holder_classes), the column which is supposed to hold the
> references
> to Class1 or Class2 objects are NULL.
>
> public class OpenJPA_Test {
>
>    private static HolderDao holderDao =
> EntityDaoFactory.inst().getHolderDao();
>
>    /**
>     * @param args the command line arguments
>     */
>    public static void main(String[] args) {
>        // TODO code application logic here
>        Holder h = new Holder();
>        LinkedList<IInterface> list = new LinkedList<IInterface>();
>
>        Class1 c1 = new Class1();
>        Class2 c2 = new Class2();
>
>        list.add(c1);
>        list.add(c2);
>
>        h.setClasses(list);
>        holderDao.create(h);
>
>    }
> }
>
>
> @ManagedInterface
> public interface IInterface {
>    @Id
>    @GeneratedValue(strategy = GenerationType.IDENTITY)
>    int getId();
>    void setId(int id);
> }
>
>
> @Entity
> @Table(name = "class1")
> public class Class1 implements IInterface {
>
>    @Id
>    private int id;
>    @Basic
>    private int number;
>
>    public Class1() {
>    }
>
>    @Override
>    public int getId() {
>        return id;
>    }
>
>    @Override
>    public void setId(int id) {
>        this.id = id;
>    }
>
>    public int getNumber() {
>        return number;
>    }
>
>    public void setNumber(int number) {
>        this.number = number;
>    }
> }
>
>
>
> @Entity
> @Table(name = "class2")
> public class Class2 implements IInterface {
>
>    @Id
>    private int id;
>    @Basic
>    private String text;
>
>    public Class2(){
>    }
>
>    public String getText() {
>        return text;
>    }
>
>    public void setText(String text) {
>        this.text = text;
>    }
>
>    public int getId() {
>        return id;
>    }
>
>    public void setId(int id) {
>        this.id = id;
>    }
> }
>
>
>
> @Entity
> @Table(name = "holder")
> public class Holder {
>
>    @Id
>    @GeneratedValue(strategy = GenerationType.IDENTITY)
>    private int id;
>    @OneToMany(cascade = CascadeType.ALL)
>    private Collection<IInterface> classes;
>
>    public Holder(){
>    }
>
>    public Collection<IInterface> getClasses() {
>        return classes;
>    }
>
>    public void setClasses(Collection<IInterface> classes) {
>        this.classes = classes;
>    }
>
>    public int getId() {
>        return id;
>    }
>
>    public void setId(int id) {
>        this.id = id;
>    }
> }
>

-- 
*Rick Curtis*