在设置HttpSession的属性后,它变为Null

时间:2014-09-22 09:55:25

标签: java session java-ee jpa ejb

我正在开发一个注册操作,用户输入他的电子邮件和密码按下提交(createUser方法)按钮,用户实体获取持久化并在HttpSession中设置用户的ID,然后他移动到下一个jsf,在那里输入学校对象(实体)的信息。我的问题是用户坚持但学校不是。这是我的代码:

public CreateBn() {
    user = new User();
    school = new School();
    adress = new Adresse();
    school.setAdresse(adress);
    facesContext = FacesContext.getCurrentInstance();
    session = (HttpSession) facesContext.getExternalContext().getSession(false);
}

public String createUser() {
    initialiserDateInscription();
    session.setAttribute("UserId", user.getId());
    //System.out.println((BigInteger) session.getAttribute("UserId"));
    userPr.createUser(user);
    return SHCOOL_INSCRIPTION;
}

public String createSchool() {
    BigInteger userId = (BigInteger) session.getAttribute("UserId");
    System.out.println("MEHDI : " + userId);
    try {
        User userTemp = userPr.getUserById(userId);// Here is the problem 
        school.setUser(userTemp);
    } catch (Exception e) {
        e.printStackTrace();
    }
    session.setAttribute("SchoolId", school.getId());
    school.setAdresse(adress);
    schoolPr.createSchool(school);
    return INSCRIPTION_RETURN;
}

正如您所看到的,我根据会话中保存的UserId获取了User实体,但我什么都没得到,它说:

javax.persistence.NoResultException: getSingleResult() did not retrieve any entities.

更多信息,我在这里。那我怎么解决这个问题呢?

@Entity
@Table(schema = "school", name = "school")
public class School implements Serializable {

private static final long serialVersionUID = 1L;

@Id
private BigInteger id;

private String name;

@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "userId")
private User user;

@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.PERSIST)
@JoinColumn(name = "adressId")
private Adresse adresse;

EJB

@Stateless
public class UserPr {

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


public void createUser(User user) throws RuntimeException{
    try {
        em.persist(user);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public User getUserById(BigInteger UserId) throws Exception{
    try{
        return em.createNamedQuery("User.findById", User.class).setParameter("id", UserId).getSingleResult();
    }catch(Exception e){
        e.printStackTrace();
    }
    return null;
}

}

@NamedQueries({
@NamedQuery(name="User.findById", query="SELECT u FROM User u WHERE u.id = :id")
})
public class User implements Serializable {

2 个答案:

答案 0 :(得分:0)

首先关于模式的一些评论:我认为学校不应该与用户建立OneToOne关系。更可能是OneToMany关系。也许你不想为Adresse建立一个单独的Entity,它可能是一个Embeddable。

你的坚持如何建立?你是如何管理交易的?您看到的问题可能是由事务边界引起的。如何完全调用createUser和createSchool方法?

答案 1 :(得分:0)

尝试更改获取方式并设置userId。

而不是这一行

session.setAttribute("UserId", user.getId());

试试这个

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> sessionMap = externalContext.getSessionMap();
sessionMap.put("UserId", user.getId());

而不是这个

BigInteger userId = (BigInteger) session.getAttribute("UserId");

试试这个

BigInteger userId  = (BigInteger) sessionMap.get("UserId");