我实现了两个类,即User.java和UserDetail.java,并且我在它们之间创建了一对一映射, 片段是
User.java
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="fname")
private String fname;
@Column(name="sname")
private String sname;
@Column(name="lname")
private String lname;
@Column(name="gender")
private String gender;
@Column(name="dob")
private String dob;
@Column(name="email")
private String email;
@Column(name="phone")
private String phone;
@Column(name="learningcenter")
private String learningcenter;
@Column(name="country")
private String country;
@Column(name="countryoforigin")
private String countryoforigin;
public String getCountryoforigin() {
return countryoforigin;
}
public void setCountryoforigin(String countryoforigin) {
this.countryoforigin = countryoforigin;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="user_detail_id")
private UserDetail userDetail;
UserDetail.java
@Entity
@Table(name="user_detail")
public class UserDetail {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_language")
private String first_language;
@Column(name="second_language")
private String second_language;
@Column(name="levelofenglish")
private String levelofenglish;
@Column(name="levelofenglishyesorno")
private String levelofenglishyesorno;
@Column(name="levelofinternet")
private String levelofinternet;
@Column(name="cos1")
private String cos1;
@Column(name="cos2")
private String cos2;
@Column(name="proof")
private String proof;
@Column(name="yesq")
private String yesq;
@Column(name="certi1")
private String certi1;
@Column(name="employee")
private String employee;
@Column(name="oldornew")
private String oldornew;
@Column(name="vol")
private String vol;
@Column(name="commitments")
private String commitments;
@Column(name="profession")
private String profession;
public String getLevelofenglishyesorno() {
return levelofenglishyesorno;
}
public void setLevelofenglishyesorno(String levelofenglishyesorno) {
this.levelofenglishyesorno = levelofenglishyesorno;
}
@Column(name="howuknow")
private String howuknow;
@Column(name="othercomments")
private String othercomments;
@Column(name="expectation")
private String expectation;
@Column(name="status")
private String status;
@OneToOne(mappedBy = "userDetail")
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
我已经创建了所有数据库表并创建了register.jsp页面以获取所有内容并将其传递给控制器 RegisterController.java
SessionFactory sessionFactory=new Configuration().configure("hibernate.cfg.xml")
.addAnnotatedClass(User.class)
.addAnnotatedClass(UserDetail.class).buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
boolean flag=false;
try
{
User user=new User(fname.toUpperCase(),sname.toUpperCase(),lname.toUpperCase(),gender,dob,email,phone,learningcenter,country,countryoforigin);
UserDetail userDetail=new UserDetail(first_language,second_language,levelofenglish,levelofenglishyesorno,levelofinternet,cos1,cos2,
proof,yesq,certi1,employee,oldornew,vol,commitments,profession,howuknow,othercomments,expectation,"null");
user.setUserDetail(userDetail);
session.beginTransaction();
session.save(user);
userDetail.setUser(user);
int id=user.getId();
System.out.println(id+"is generated");
List<User> userList=(List<User>)session.createQuery("from User u where u.id=:id")
.setParameter("id",id).list();
for(User u:userList)
{
System.out.println(u.toString());
flag=true;
}
model.addAttribute("user",user);
model.addAttribute("userDetail",userDetail);
session.getTransaction().commit();
session.close();
}
catch (Exception e)
{
System.out.println(e);
}
if(flag)
{
return "success";
}
else
{
return "failure";
}
执行该应用程序后,我能够注册该用户,以便它反映在数据库表中。 我想知道的是通过jsp代码获取User和UserDetail的数据, 但我无法获取userDetail内容
<div class="jumbotron text-xs-center">
<c:forEach var="item" items="${user}">
<h1 class="display-3">Login Successfull!${item.fname}</h1>
${item}
</c:forEach>
</div>
在上述情况下,我可以获取“ user”,但不能获取“ userDetail”。我想在JSP代码中获取用户以及userDetail。请帮助
答案 0 :(得分:1)
在代码中,您正在设置要创建的用户和用户详细信息,而不是从数据库中获取用户的详细信息。
User user=new User(fname.toUpperCase(),sname.toUpperCase(),lname.toUpperCase(),gender,dob,email,phone,learningcenter,country,countryoforigin);
UserDetail userDetail=new UserDetail(first_language,second_language,levelofenglish,levelofenglishyesorno,levelofinternet,cos1,cos2,
proof,yesq,certi1,employee,oldornew,vol,commitments,profession,howuknow,othercomments,expectation,"null");
user.setUserDetail(userDetail);
session.beginTransaction();
session.save(user);
userDetail.setUser(user);
int id=user.getId();
System.out.println(id+"is generated");
List<User> userList=(List<User>)session.createQuery("from User u where u.id=:id")
.setParameter("id",id).list();
for(User u:userList)
{
System.out.println(u.toString());
flag=true;
}
model.addAttribute("user",user);
model.addAttribute("userDetail",userDetail);
使用以下代码检查:
<div class="jumbotron text-xs-center">
<c:forEach var="item" items="${user}">
<h1 class="display-3">Login Successfull!${item.userDetail}</h1>
${item}
</c:forEach>
</div>