You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Romain Manni-Bucau <rm...@gmail.com> on 2011/07/21 06:59:47 UTC

openjpa 2 ManyToOne in tomcat

Hi, i'm in tomcat and i have a ManyToOne relation.

I get a NPE using the relation between both entities (Post of a blog and its
comments).

It seems to work in standalone but not Tomcat so i guess there is something
specific, can you help me to look where i should? Does it need some Jndi
things?

I enhanced entities with the maven plugin like it:

<plugin>
        <groupId>org.apache.openjpa</groupId>
        <artifactId>openjpa-maven-plugin</artifactId>
        <version>2.2.0-SNAPSHOT</version>
        <configuration>
          <includes>org/superbiz/model/*.class</includes>
          <addDefaultConstructor>true</addDefaultConstructor>
          <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
        </configuration>
        <executions>
          <execution>
            <id>enhancer</id>
            <phase>process-classes</phase>
            <goals>
              <goal>enhance</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa</artifactId>
            <version>2.0.1</version>
          </dependency>
        </dependencies>
      </plugin>




Here are entities:

============ Post.java

package org.superbiz.rest.model;

import javax.annotation.PostConstruct;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@NamedQueries({
    @NamedQuery(name = "post.list", query = "select p from Post p")
})
@XmlRootElement(name = "post")
public class Post {
    @Id @GeneratedValue private long id;
    @Past private Date created;
    @NotNull @Size(min = 1) private String title;
    @NotNull @Size(min = 1) @Lob private String content;
    @ManyToOne @Valid private User user;
    @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) private
List<Comment> comments;

    @PostConstruct public void create() {
        created = new Date();
    }

    public Post title(final String title) {
        this.title = title;
        return this;
    }

    public Post content(final String content) {
        this.content = content;
        return this;
    }

    public Post user(final User user) {
        this.user = user;
        return this;
    }

    public long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<Comment> getComments() {
        if (comments == null) {
            comments = new ArrayList<Comment> ();
        }
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    public void addComment(final Comment comment) {
        getComments().add(comment);
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }
}

============ Comment.java

package org.superbiz.rest.model;

import javax.annotation.PostConstruct;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;

@Entity
@NamedQueries({
    @NamedQuery(name = "comment.list", query = "select c from Comment c")
})
@XmlRootElement(name = "comment")
public class Comment {
    @Id @GeneratedValue private long id;
    @Past private Date created;
    @NotNull @Size(min = 1) private String author;
    @NotNull @Size(min = 1) @Lob private String content;
    @ManyToOne(optional = false) @Valid private Post post;

    @PostConstruct public void create() {
        created = new Date();
    }

    public Comment author(final String author) {
        this.author = author;
        return this;
    }

    public Comment content(final String content) {
        this.content = content;
        return this;
    }

    public long getId() {
        return id;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }
}


- Romain

Re: openjpa 2 ManyToOne in tomcat

Posted by Rick Curtis <cu...@gmail.com>.
Romain -

> I get a NPE using the relation between both entities (Post of a blog and
its comments).
Are you able to find your Post / Comments if you are to execute an
EntityManager.find(...) operation?

> It seems to work in standalone but not Tomcat so i guess there is
something specific, can you help me to look where i should? Does it need
some Jndi things?
This leads me to believe that there is some sort of connection configuration
issue. How are you configuring your DB in JSE vs Tomcat?

Thanks,
Rick

On Wed, Jul 20, 2011 at 11:59 PM, Romain Manni-Bucau
<rm...@gmail.com>wrote:

> Hi, i'm in tomcat and i have a ManyToOne relation.
>
> I get a NPE using the relation between both entities (Post of a blog and
> its
> comments).
>
> It seems to work in standalone but not Tomcat so i guess there is something
> specific, can you help me to look where i should? Does it need some Jndi
> things?
>
> I enhanced entities with the maven plugin like it:
>
> <plugin>
>        <groupId>org.apache.openjpa</groupId>
>        <artifactId>openjpa-maven-plugin</artifactId>
>        <version>2.2.0-SNAPSHOT</version>
>        <configuration>
>          <includes>org/superbiz/model/*.class</includes>
>          <addDefaultConstructor>true</addDefaultConstructor>
>          <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
>        </configuration>
>        <executions>
>          <execution>
>            <id>enhancer</id>
>            <phase>process-classes</phase>
>            <goals>
>              <goal>enhance</goal>
>            </goals>
>          </execution>
>        </executions>
>        <dependencies>
>          <dependency>
>            <groupId>org.apache.openjpa</groupId>
>            <artifactId>openjpa</artifactId>
>            <version>2.0.1</version>
>          </dependency>
>        </dependencies>
>      </plugin>
>
>
>
>
> Here are entities:
>
> ============ Post.java
>
> package org.superbiz.rest.model;
>
> import javax.annotation.PostConstruct;
> import javax.persistence.CascadeType;
> import javax.persistence.Entity;
> import javax.persistence.GeneratedValue;
> import javax.persistence.Id;
> import javax.persistence.Lob;
> import javax.persistence.ManyToOne;
> import javax.persistence.NamedQueries;
> import javax.persistence.NamedQuery;
> import javax.persistence.OneToMany;
> import javax.validation.Valid;
> import javax.validation.constraints.NotNull;
> import javax.validation.constraints.Past;
> import javax.validation.constraints.Size;
> import javax.xml.bind.annotation.XmlRootElement;
> import java.util.ArrayList;
> import java.util.Date;
> import java.util.List;
>
> @Entity
> @NamedQueries({
>    @NamedQuery(name = "post.list", query = "select p from Post p")
> })
> @XmlRootElement(name = "post")
> public class Post {
>    @Id @GeneratedValue private long id;
>    @Past private Date created;
>    @NotNull @Size(min = 1) private String title;
>    @NotNull @Size(min = 1) @Lob private String content;
>    @ManyToOne @Valid private User user;
>    @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) private
> List<Comment> comments;
>
>    @PostConstruct public void create() {
>        created = new Date();
>    }
>
>    public Post title(final String title) {
>        this.title = title;
>        return this;
>    }
>
>    public Post content(final String content) {
>        this.content = content;
>        return this;
>    }
>
>    public Post user(final User user) {
>        this.user = user;
>        return this;
>    }
>
>    public long getId() {
>        return id;
>    }
>
>    public void setId(long id) {
>        this.id = id;
>    }
>
>    public String getTitle() {
>        return title;
>    }
>
>    public void setTitle(String title) {
>        this.title = title;
>    }
>
>    public String getContent() {
>        return content;
>    }
>
>    public void setContent(String content) {
>        this.content = content;
>    }
>
>    public User getUser() {
>        return user;
>    }
>
>    public void setUser(User user) {
>        this.user = user;
>    }
>
>    public List<Comment> getComments() {
>        if (comments == null) {
>            comments = new ArrayList<Comment> ();
>        }
>        return comments;
>    }
>
>    public void setComments(List<Comment> comments) {
>        this.comments = comments;
>    }
>
>    public void addComment(final Comment comment) {
>        getComments().add(comment);
>    }
>
>    public Date getCreated() {
>        return created;
>    }
>
>    public void setCreated(Date created) {
>        this.created = created;
>    }
> }
>
> ============ Comment.java
>
> package org.superbiz.rest.model;
>
> import javax.annotation.PostConstruct;
> import javax.persistence.Entity;
> import javax.persistence.GeneratedValue;
> import javax.persistence.Id;
> import javax.persistence.Lob;
> import javax.persistence.ManyToOne;
> import javax.persistence.NamedQueries;
> import javax.persistence.NamedQuery;
> import javax.validation.Valid;
> import javax.validation.constraints.NotNull;
> import javax.validation.constraints.Past;
> import javax.validation.constraints.Size;
> import javax.xml.bind.annotation.XmlRootElement;
> import java.util.Date;
>
> @Entity
> @NamedQueries({
>    @NamedQuery(name = "comment.list", query = "select c from Comment c")
> })
> @XmlRootElement(name = "comment")
> public class Comment {
>    @Id @GeneratedValue private long id;
>    @Past private Date created;
>    @NotNull @Size(min = 1) private String author;
>    @NotNull @Size(min = 1) @Lob private String content;
>    @ManyToOne(optional = false) @Valid private Post post;
>
>    @PostConstruct public void create() {
>        created = new Date();
>    }
>
>    public Comment author(final String author) {
>        this.author = author;
>        return this;
>    }
>
>    public Comment content(final String content) {
>        this.content = content;
>        return this;
>    }
>
>    public long getId() {
>        return id;
>    }
>
>    public void setId(long id) {
>        this.id = id;
>    }
>
>    public String getAuthor() {
>        return author;
>    }
>
>    public void setAuthor(String author) {
>        this.author = author;
>    }
>
>    public String getContent() {
>        return content;
>    }
>
>    public void setContent(String content) {
>        this.content = content;
>    }
>
>    public Post getPost() {
>        return post;
>    }
>
>    public void setPost(Post post) {
>        this.post = post;
>    }
>
>    public Date getCreated() {
>        return created;
>    }
>
>    public void setCreated(Date created) {
>        this.created = created;
>    }
> }
>
>
> - Romain
>



-- 
*Rick Curtis*