我正在尝试仅使用 JPA 和 JSF 设计用户管理模块,而无需 EJB。我得到java.io.NotSerializableException: org.eclipse.persistence.internal.jpa.EntityManagerImpl我有带有以下代码的用户控制器:
@ManagedBean
@ViewScoped
public class UserController implements Serializable {
public static final Logger logger = Logger.getLogger(UserController.class.getName());
EntityManagerFactory emf;
EntityManager em;
EntityTransaction tx;
private List<Users> users = new ArrayList<Users>();
private List<Groups> groups = new ArrayList<Groups>();
Groups group = new Groups();
Users user = new Users();
public String[] usergroups;
boolean editGroup = false;
boolean addGroup = false;
boolean editUser = false;
boolean addUser = false;
//getter and setter
}
用户实体:
@Entity
@Table(/*catalog = "GIS",*/ schema = "useradministration")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findByUserid", query = "SELECT u FROM Users u WHERE u.userid = :userid"),
@NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
@NamedQuery(name = "Users.findByUserpassword", query = "SELECT u FROM Users u WHERE u.userpassword = :userpassword"),
@NamedQuery(name = "Users.findByFirstname", query = "SELECT u FROM Users u WHERE u.firstname = :firstname"),
@NamedQuery(name = "Users.findByMiddlename", query = "SELECT u FROM Users u WHERE u.middlename = :middlename"),
@NamedQuery(name = "Users.findByLastname", query = "SELECT u FROM Users u WHERE u.lastname = :lastname")})
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="useradministration.users_userid_seq",
sequenceName="useradministration.users_userid_seq",
allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator="useradministration.users_userid_seq")
@Basic(optional = false)
@Column(nullable = false)
private Integer userid;
@Size(max = 50)
@Column(length = 50)
private String username;
@Size(max = 200)
@Column(length = 200)
private String userpassword;
@Size(max = 100)
@Column(length = 100)
private String firstname;
@Size(max = 100)
@Column(length = 100)
private String middlename;
@Size(max = 100)
@Column(length = 100)
private String lastname;
@ManyToMany(mappedBy = "usersList", fetch = FetchType.LAZY)
private List<Groups> groupsList;
}
集团实体:
package com.ncell.user;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
*
* @author suamatya
*/
@Entity
@Table(schema = "useradministration")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Groups.findAll", query = "SELECT g FROM Groups g"),
@NamedQuery(name = "Groups.findByGroupid", query = "SELECT g FROM Groups g WHERE g.groupid = :groupid"),
@NamedQuery(name = "Groups.findByGroupname", query = "SELECT g FROM Groups g WHERE g.groupname = :groupname"),
@NamedQuery(name = "Groups.findByGroupdesc", query = "SELECT g FROM Groups g WHERE g.groupdesc = :groupdesc")})
public class Groups implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="useradministration.groups_groupid_seq",
sequenceName="useradministration.groups_groupid_seq",
allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator="useradministration.groups_groupid_seq")
@Basic(optional = false)
@Column(nullable = false)
private Integer groupid;
@Size(max = 50)
@Column(length = 50)
private String groupname;
@Size(max = 200)
@Column(length = 200)
private String groupdesc;
@JoinTable(name = "useradministration.usergroups", joinColumns = {
@JoinColumn(name = "userid", referencedColumnName = "groupid", nullable = false)}, inverseJoinColumns = {
@JoinColumn(name = "userid", referencedColumnName = "userid", nullable = false)})
@ManyToMany(fetch = FetchType.LAZY)
private List<Users> usersList;
}
所以问题是:有没有没有 EJB 的 JPA 和 JSF 的例子?我已经使控制器可序列化,因为它是视图范围的。