0

我有 OpenJPA 和实体的应用程序,User并且UserHasRoles

public class User implements Serializable {
 @Id
 @GeneratedValue
 @Basic(optional = false)
 @Column(name = "id_user")
 private Integer idUser;
 @Size(max = 8)
 @Column(name = "login")
 private String login;
 @Size(max = 64)
 @Column(name = "password")
 private String password;
 @OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch=FetchType.LAZY)
 private List<UserHasRoles> userHasRolesList;

 //getters, setters ...
}

public class UserHasRoles implements Serializable {
 @EmbeddedId
 protected UserHasRolesPK userHasRolesPK;
 @Basic(optional = false)
 @NotNull
 @Size(min = 1, max = 45)
 @Column(name = "role_name")
 private String roleName;
 @JoinColumn(name = "id_user", referencedColumnName = "id_user", insertable = false, updatable = false)
 @ManyToOne(optional = false)
 private User user;

 //getters, setters ...
}

当我运行此代码时,我得到 NullPointerException:

User user = new User(); //utworzenie użytkownika i dodanie do bazy    
user.setLogin(registerLog);
user.setPassword(registerPass);
userFacade.create(user);

UserHasRoles userHasRoles = new UserHasRoles(user.getIdUser(), new Short("2"));
userHasRoles.setRoleName("admin");
userHasRoles.setUser(user);
userHasRolesFacade.create(userHasRoles);

ArrayList<UserHasRoles> list = new ArrayList<UserHasRoles>();       //1
list.add(userHasRoles);                                             //1    
user.setUserHasRolesList(list);                                     //1 
userFacade.edit(user);                                              //1 


User u = userFacade.find(user.getIdUser());
List l = u.getUserHasRolesList();

l.get(0); //i get there NullPointerException, because l is null

为什么在持久化和合并对象之后(UserHasRoles在上面的评论中添加 - 1 列表)当我找到对象时我得到 null ?


我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="CollDocPU" transaction-type="JTA">
<jta-data-source>java:openejb/Resource/myDatabase</jta-data-source>
<non-jta-data-source>java:openejb/Resource/myDatabaseUnmanaged</non-jta-data-source>
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>model.entity.Course</class>
<class>model.entity.CourseHasLecturer</class>
<class>model.entity.File</class>
<class>model.entity.Lecturer</class>
<class>model.entity.Mail</class>
<class>model.entity.Roles</class>
<class>model.entity.Solution</class>
<class>model.entity.Student</class>
<class>model.entity.StudentAddSolution</class>
<class>model.entity.StudentAtCourse</class>
<class>model.entity.Task</class>
<class>model.entity.User</class>
<class>model.entity.UserHasRoles</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:11080/me?zeroDateTimeBehavior=convertToNull"/>
  <property name="javax.persistence.jdbc.password" value="pass"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.user" value="me"/>
  <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
</properties>

4

2 回答 2

2

FetchType.LAZY仅当您调用该方法时才加载列表,getUserHasRolesList()并且此方法似乎在“用户”类中

@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch=FetchType.LAZY)
 private List<UserHasRoles> userHasRolesList;

您可以将 fetch 更改为 EAGER,然后列表将与其余字段一起加载。

于 2013-10-15T12:51:01.047 回答
1

问题在于实体的生命周期。所有实体都必须重新附加到持久性上下文。

为了解决它,我必须在以下位置制作getUserHasRolesList方法userFacade

public List<UserHasRoles> getUserHasRolesList(User us){

    us = em.merge(us);      //here i re-attached entity

    return us.getUserHasRolesList();
}

同样的问题在这里:OpenJPA - 延迟获取不起作用,在这里:什么是延迟策略以及它是如何工作的?

于 2013-10-22T21:33:43.393 回答