我使用此向导从我的数据库创建实体类。有些表尚未转换为类,但有一些属性可以识别关系。
this is my db ERD(mysql)
这是用户实体类(属性)
@Entity
@Table(name = "user")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
@NamedQuery(name = "User.findByOid", query = "SELECT u FROM User u WHERE u.oid = :oid"),
@NamedQuery(name = "User.findByUsername", query = "SELECT u FROM User u WHERE u.username = :username"),
@NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password"),
@NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email"),
@NamedQuery(name = "User.findByAddress", query = "SELECT u FROM User u WHERE u.address = :address"),
@NamedQuery(name = "User.findBySince", query = "SELECT u FROM User u WHERE u.since = :since")})
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "oid")
private Integer oid;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 15)
@Column(name = "username")
private String username;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 15)
@Column(name = "password")
private String password;
// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "email")
private String email;
@Size(max = 50)
@Column(name = "address")
private String address;
@Basic(optional = false)
@NotNull
@Column(name = "since")
@Temporal(TemporalType.DATE)
private Date since;
@JoinTable(name = "favorite", joinColumns = {
@JoinColumn(name = "user_oid", referencedColumnName = "oid")}, inverseJoinColumns = {
@JoinColumn(name = "wheelchair_oid", referencedColumnName = "oid")})
@ManyToMany
private List<Wheelchair> wheelchairList;
@ManyToMany(mappedBy = "userList1")
private List<Wheelchair> wheelchairList1;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "senderOid")
private List<Comment> commentList;
@JoinColumn(name = "role_oid", referencedColumnName = "oid")
@ManyToOne(optional = false)
private Role roleOid;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "userOid")
private List<Orthopedy> orthopedyList;
public User() {
}
...
我无法理解: OWN连接表在哪里? 为什么我有userList1和wheelchairList1?它应该识别OWN表吗?在这种情况下,我可以在这里重命名,或者我必须在一些xml文件中重命名它?
的原因 @OneToMany(cascade = CascadeType.ALL, mappedBy = "userOid")
private List<Orthopedy> orthopedyList;
? 它应该是OneToOne ......
此外,“JSF from entities class”向导创建CRUD操作来管理用户,如何管理连接表?我需要在控制器中写一些东西吗?
你能告诉我一些可以学到这些资源的资源吗?非常感谢你
答案 0 :(得分:1)
创建实体时,它为所有具有主键的表创建类 但不适用于拥有多对多关系的表格。它由父类管理,并作为列表进行维护。
这是我管理多对多的SubjectFaculty表的代码,其中包含教师和科目的详细信息
为学院分配科目
public void assignFacultyToSubject(String facultyUname, Integer subjectId) {
try {
Subject oSubject = em.find(Subject.class, subjectId);
Faculty oFaculty = em.find(Faculty.class, facultyUname);
College oCollege = em.find(College.class, oFaculty.getCollegeUname().getCollegeUname());
List<Faculty> lstFaculty = oSubject.getFacultyList();
List<Subject> lstSubject = oFaculty.getSubjectList();
if (!lstSubject.contains(oSubject)) {
lstFaculty.add(oFaculty);
lstSubject.add(oSubject);
oSubject.setFacultyList(lstFaculty);
oFaculty.setSubjectList(lstSubject);
em.merge(oSubject);
em.getEntityManagerFactory().getCache().evictAll();
} else {
System.out.println("Entry Already Found");
}
} catch (Exception e) {
System.out.println("Error :- " + e.getMessage());
}
}
删除主题和教师详细信息表格多对多表
@Override
public void removeFacultySubject(String facultyUname, Integer subjectId) {
try {
Subject oSubject = em.find(Subject.class, subjectId);
Faculty oFaculty = em.find(Faculty.class, facultyUname);
List<Subject> lstSubject = oFaculty.getSubjectList();
List<Faculty> lsFaculty = oSubject.getFacultyList();
lstSubject.remove(oSubject);
lsFaculty.remove(oFaculty);
em.merge(oSubject);
} catch (Exception e) {
System.out.println("Error :- " + e.getMessage());
}
}