持久化类在ID上给出null值

时间:2013-01-24 10:56:37

标签: java jpa netbeans glassfish entity

我正在尝试保留一个User-class,其中包含2个列表:一个用户可以查看哪些用户正在关注该用户,另一个用户可以查看特定用户关注的用户。

User.java:

@Entity
@XmlRootElement
@Table(name="Users")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String web;
    private String bio;
    @OneToMany(cascade=CascadeType.ALL)
    private Collection<User> followers = new ArrayList();
    @OneToMany(cascade=CascadeType.ALL)
    private Collection<User> following = new ArrayList();

我有一个名为UserDAOJPA.java的类,用于创建和修改用户:

@Alternative
@Stateless
public class UserDAOJPA implements Serializable, IUserDAO {

    @PersistenceContext(unitName = "KwetterSOAPPU")
    private EntityManager em;

    @Override
    public void create(User user) {
        em.persist(user);
    }
@Override
    public List<User> getFollowers(User user) {
        List<User> followers;
        followers = (List<User>) user.getFollowers();
        return followers;
    }

    @Override
    public void addFollower(User user, User follower)
    {
        user.addFollower(follower);
        em.merge(user);
    }

    @Override
    public List<User> getFollowing(User user) {
        List<User> following;
        following = (List<User>) user.getFollowing();
        return following;
    }

    @Override
    public void addFollowing(User user, User following) {
        user.addFollower(following);
        em.merge(user);
    }
    @PostConstruct
    private void initUsers() {
        User u1 = new User("Hans", "http", "geboren 1");
        User u2 = new User("Frank", "httpF", "geboren 2");
        User u3 = new User("Tom", "httpT", "geboren 3");
        User u4 = new User("Sjaak", "httpS", "geboren 4");

        this.create(u1);
        this.create(u2);
        this.create(u3);
        this.create(u4);

        this.addFollowing(u1, u2);
        this.addFollower(u2, u1);
        this.addFollowing(u1, u3);
        this.addFollower(u3, u1);
        this.addFollowing(u1, u4);
        this.addFollower(u4, u1);

我自己的猜测是,在查看用户集时,我在User.java类中缺少正确的注释。

错误消息:

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'FOLLOWING_ID'  cannot accept a NULL value.
Error Code: -1
Call: INSERT INTO Users_Users (followers_ID, User_ID) VALUES (?, ?)
    bind => [2 parameters bound]
Query: DataModifyQuery(name="followers" sql="INSERT INTO Users_Users (followers_ID, User_ID) VALUES (?, ?)")

2 个答案:

答案 0 :(得分:2)

用户与其关注者之间的关系是双向的多对多关系,而不是两个一对多的关系,因此您需要将其映射为:

@ManyToMany(mappedBy = "following")
private Collection<User> followers = new ArrayList<>();

@ManyToMany
@JoinTable(name = "followers",
    joinColumn = @Column(name = "follower_id"),
    inverseJoinColumn = @Column(name = "following_id"))
private Collection<User> following = new ArrayList<>();

另请注意,在引用逻辑“拥有”实体时,通常应使用级联。在你的情况下它是没有意义的,因为User s彼此不“拥有”。

答案 1 :(得分:1)

只是猜测,因为我没有测试代码的环境,但我认为您需要更改create方法以从EntityManager.merge类返回返回值并使用结果,如下所示:

...

public User create(User user) {
    return em.merge(user);
}

...

User user = this.create(new User(...));
User follower = this.create(new User(...));
this.addFollower(user, follower);

错误的原因是对Follower的id的引用是未定义的,因为你没有返回合并的对象状态,你已经将它保留了。