在hibernate中连接表时出错

时间:2016-12-31 16:05:19

标签: spring hibernate join

我想在hibernate中加入表但是我收到错误“HTTP状态500 - 请求处理失败;嵌套异常是org.hibernate.PropertyAccessException:无法通过com.spring.entity的反射getter获取字段值。 StudentInfo.faculty“

faculty.class

@Entity
@Table(name="faculty")
public class Faculty {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="faculty_id")
private int faculty_id;


@Column(name="faculty")
private String faculty;


@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FACULTY_ID",referencedColumnName ="FACULTY_ID")
private StudentInfo studentInfo;

//getter and setter code

StudentInfo.class

@Entity
@Table (name="studentinfo")
public class StudentInfo implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;


@Column(name="year_id")
private int year;


@Column(name="faculty_id")
private int faculty;

@NotEmpty(message="First Name cannot be empty")
@Column(name="firstname")
private String firstName;

@NotEmpty(message="Last Name cannot be empty")
@Column(name="lastname")
private String lastName;

@Column(name="contact_no")
private String contact_No;

@Column(name="address")
private String address;


@Email(message="Enter a valid email address")
@Column(name="email")
private String email;


@Column(name="images")
private String images;

@OneToOne(mappedBy = "studentInfo")
private Faculty fac;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}


public int getFaculty() {
    return faculty;
}

public void setFaculty(int faculty) {
    this.faculty = faculty;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getContact_No() {
    return contact_No;
}

public void setContact_No(String contact_No) {
    this.contact_No = contact_No;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getImages() {
    return images;
}

public void setImages(String images) {
    this.images = images;
} 

public Faculty getFac() {
    return fac;
}
public void setFac(Faculty fac) {
    this.fac = fac;
 }

@Override
public String toString() {
    return "StudentInfo{" + "id=" + id + ", year=" + year + ", faculty=" + faculty + ", firstName=" + firstName + ", lastName=" + lastName + ", contact_No=" + contact_No + ", address=" + address + ", email=" + email + ", images=" + images + '}';
}

我想从studentinfo类获取教师名称,但是在使用此代码时出现错误。

@Controller类

@RequestMapping("showFormForView")
public String viewStudent(@RequestParam("studentId") int theId, Model model)   {

  StudentInfo theStudent=studentService.getStudent(theId);
  model.addAttribute("addstd",theStudent);
  return "viewstudent";
}

StudentDAOImplemetntation class

public StudentInfo getStudent(int theId) {
Session currentSession=sessionFactory.getCurrentSession();
StudentInfo theStudent=(StudentInfo)     currentSession.get(StudentInfo.class,theId);

return theStudent;        
}

JSP页面

<p class="msgs">

    <br>
    First Name : ${addstd.firstName} <br>
    Last Name :   ${addstd.lastName} <br>
    Contact No : ${addstd.contact_No} <br>
    Address : ${addstd.address} <br>
    Email : ${addstd.email} <br>
    Faculty:${addstd.fac.faculty}<br>         
</p>

我无法在此代码中获得引人注目的名称。

0 个答案:

没有答案