1

我有一个在 Glassfish 3.1.1 下运行的相当简单的 servlet 应用程序,并带有一个 Derby 10.5.3.0 数据库。我已经使用 JPA 注释创建了一个安全领域,并且能够成功登录。当我尝试访问用户实体时,我的问题就来了。JPA/Hibernate 似乎在反复读取用户表,直到它抛出 StackOverflowError 异常。我已经尝试注释掉登录代码,但是在尝试访问用户表时仍然会出现异常,所以我很确定问题一定出在我的 JPA 注释上。有人可以帮忙吗?

用户.java:

package com.optasense.owr.relay.domain.user;

import static com.optasense.owr.relay.domain.user.Role.ROLE.SITE_USER;

import java.io.Serializable; 
import java.util.HashSet;

@Entity(name="USER_TABLE")
public class User implements Serializable {

private static final long serialVersionUID = -1907443883886809564L;

@Id
private String userName;

@Column(length=32,columnDefinition="VARCHAR(32)")
private String password;

private String deviceID;

@ManyToMany
private Set<Site> sites;

@OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL, mappedBy="user")
private Role role;

public User() {
}

public User(String userName, String password) {
    this(userName, password , SITE_USER);
}

public User(String userName, String password, ROLE role) {
    this.userName = userName;
    this.password = password;
    this.role = new Role(role, this);
}

public String getUserName() {
    return userName;
}

public void setUsername(String userName) {
    this.userName = userName;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPassword() {
    return password;
}

public boolean isPassword(String password) {
    return getPassword().equals(password);
}

public String getDeviceID() {
    return deviceID;
}

public void setDeviceID(String deviceID) {
    this.deviceID = deviceID;
}

public void clearDeviceID() {
    this.setDeviceID(null);
}

public Set<Site> getSites() {
    if (sites == null) {
        sites = new HashSet<Site>();
    }
    return sites;
}

public void setSites(Set<Site> sites) {
    this.sites = sites;
}

public void add(Site site) {
    getSites().add(site);
}

public void remove(Site site) {
    getSites().remove(site);
}

public void setRole(Role role) {
    this.role = role;
    role.setUser(this);
}

public Role getRole() {
    return role;
}
}

角色.java:

package com.optasense.owr.relay.domain.user;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity(name="ROLE_TABLE")
public class Role implements Serializable {

private static final long serialVersionUID = -3509067216602046181L;

public static enum ROLE {
    WEB_ADMIN, SITE_ADMIN, SITE_OPERATOR, SITE_USER, SITE_VIEWER
}

@Id
@Column(name = "ROLE_NAME")
@Enumerated(EnumType.STRING)
private ROLE roleName;
@Id
@OneToOne
@JoinColumn(name = "USER_NAME")
private User user;

public Role() {
}

public Role(ROLE roleName, User user) {
    this.roleName = roleName;
    this.user = user;
}

public ROLE getRole() {
    return roleName;
}

public void setRole(ROLE roleName) {
    this.roleName = roleName;
}

public User getUser() {
    return user;
}

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

}

我的用户查找器代码(锁定模式是优化的):

private User findUser(String userName, LockModeType lockModeType) {
    User userFound = null;
    try {
        EntityManager em = getRelayPersistence().getEntityManager();
        userFound = (User)em.find(User.class, userName, lockModeType);
    } catch (Exception ignored) {
        LOGGER.debug("Unexpected exception (ignored)", ignored);
    }

    return userFound;
}
4

0 回答 0