简体   繁体   English

Hibernate返回null统一的Collection

[英]Hibernate returns null unitilzed Collection

I have two tables, users and images which need to be joined. 我有两个表,用户和图像需要连接。 there is exactly one user row in the users table to many images in the images table. 在用户表中,只有一个用户行到图像表中的许多图像。

In my users bean I have a private Set variable with a @OneToMany relationship it looks like this 在我的用户bean中,我有一个带有@OneToMany关系的私有Set变量,它看起来像这样

//Users.java
@Entity
@Table(name = "users")
@NamedQueries ({
@NamedQuery(name = "Users.getUserImage",
query("from Users as users INNER JOIN fetch users.images as image WHERE users.userId image.userId AND users.userId =: passedId")
})
public class Users
   private Set<UserImages> images;

   @OneToMany(mappedBy = "userId", fetch = FetchType.LAZY, cascade=CascadeType.ALL)
   public Set<UserImages> getImages() {
        return images;
   }

   public void setImages(Set<UserImages> images) {
        this.images = images;
  }
}

Then I have a UserImages bean which stores a bunch of data but has the fk userId which looks like so. 然后,我有一个UserImages bean,它存储了一堆数据,但是有fk userId,看起来像这样。

//UserImages.java

private Integer userId;

@Column(name = "user_id", updatable=true, nullable=false)
public Integer getUserId() {
    return userId;
}

public setUserId(Integer userId) {
    this.userId = userId;
}

I am calling the getUserImage namedQuery from within my DAO to get the resultSet. 我正在从我的DAO中调用getUserImage namedQuery以获取resultSet。

So this works well except for when a user has NO images in the UserImages table. 因此,这很好用,除非用户在UserImages表中没有图像。 (User has not uploaded any images yet). (用户尚未上传任何图像)。 I have set up a test to test everything and if a user has an image it works great I can call the getImages() method on a User and it will return a set and I can iterate through that. 我已经设置了一个测试以测试所有内容,并且如果用户拥有图像,那么它可以很好地工作,我可以在User上调用getImages()方法,它将返回一个set,我可以对其进行迭代。 But if the User does not have any images it gives me a null pointer exception right away. 但是,如果用户没有任何图像,则会立即给我一个空指针异常。

I have tried to set the Set to null in the setUserImages() method if variable this.images = null but that does not seem to work. 我试图在setUserImages()方法中将Set设置为null,如果变量this.images = null,但这似乎不起作用。 Any help would be great. 任何帮助都会很棒。 Thanks! 谢谢!

That's not the way you do things in Hibernate. 这不是您在Hibernate中执行操作的方式。 The whole point of using an ORM is that you don't have to deal with foreign keys, but with object references: 使用ORM的全部要点是,您不必处理外键,而要处理对象引用:

@Entity
public class UserImage{

    @ManyToOne
    private User user;

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

}

@Entity
public class User{

    @OneToMany(mappedBy="user")
    private Set<UserImage> images;

    public void setImages(Set<UserImage> images){this.images=images;}
    public Set<UserImage> getImages(){return this.images;}

}

About queries: don't use join. 关于查询:请勿使用联接。 Use something like this (I only use JPA, so I'm not sure about HQL): 使用类似这样的东西(我只使用JPA,所以我不确定HQL):

Select i from UserImage i where user = :user and filename like :pattern

Pass the user object and the pattern as parameter and let hibernate do the join internally. 将用户对象和模式作为参数传递,然后让hibernate在内部进行连接。 There's no use in using an ORM, if you're going to do the leg work yourself. 如果您要自己进行腿部工作,那么使用ORM没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM