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

Reply via email to