通过注释加入hibernate

时间:2016-03-25 06:38:34

标签: java mysql hibernate join

我正在尝试在hibernate中执行join,我正在使用struts2。 我正在使用注释来处理hibernate。现在我无法在两个表之间执行连接。我的第一个表是" studentprojects"其中包含pid和email。第二个表是" initialprojectdetials"其中包含pid,名称,描述......类似的其他一些字段。我必须通过在第一个表的pid周围执行连接来获取第二个表的数据。 对于这个使用此查询:

String hql="from InitialProjectDTO I join I.projectId S where I.projectId=:id";
        Query query=session.createQuery(hql);
        query.setParameter("id", id);
        mail =query.list();

其中mail是InitialProjectDTO的arraylist。 我的InitialProjectDTO是:

package edu.pma.dto;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;



@Entity
@Table(name="initialprojectdetail")
public class InitialProjectDTO {

    @Id
    @Column(name="projectId")
    @OneToMany(cascade=CascadeType.ALL)
    @JoinTable(name="studentprojects",joinColumns=@JoinColumn(name="projectId"))
    int projectId;

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

    @Column(name="description")
    String description;

    @Column(name="technology")
    String technology;

    @Column(name="guide")
    String guide;

    @Column(name="duration")
    int duration;

    @Column(name="status")
    String status;
    @Column(name="report")
    String report;

    public String getReport() {
        return report;
    }

    public void setReport(String report) {
        this.report = report;
    }

    public int getProjectId() {
        return projectId;
    }

    public void setProjectId(int projectId) {
        this.projectId = projectId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTechnology() {
        return technology;
    }

    public void setTechnology(String technology) {
        this.technology = technology;
    }

    public String getGuide() {
        return guide;
    }

    public void setGuide(String guide) {
        this.guide = guide;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

}

我的SudentProjectDTO是:

package edu.pma.dto;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="studentprojects")
public class StudentProjectDTO {

    public int getProjectId() {
        return projectId;
    }
    public void setProjectId(int projectId) {
        this.projectId = projectId;
    }
    @Id
    @Column(name="email")
    String email;
    @Column(name="projectId")
    int projectId;
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

}

这是我得到的错误:

Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: edu.pma.dto.InitialProjectDTO.projectId
Method "execute" failed for object edu.pma.actions.LoginAction@1096a56
File:   org/hibernate/cfg/annotations/CollectionBinder.java

2 个答案:

答案 0 :(得分:0)

你应该尝试使用不同的模型

@Entity
public class InitialProjectDTO {

 @OneToMany(mappedBy = "project")
 private Collection<StudentProjectDTO> students;

}

@Entity
public class StudentProjectDTO {

 @ManyToOne
 private InitialProjectDTO project;

}

使用正确的模型可以很容易地编写hql,您可能希望在这里查看示例https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

另外我建议在这里查看模型的例子。 http://viralpatel.net/blogs/hibernate-one-to-many-annotation-tutorial/

答案 1 :(得分:0)

请参阅以下示例可能对您有所帮助

@Entity
@Table(name="initialprojectdetail")
public class InitialProjectDTO {


    private Integer initialProjectDTOId;
    private Set<StudentProjectDTO > studentProjectDTO  = new HashSet<StudentProjectDTO >(0);

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "initial_projectDTO_id", unique = true, nullable = false)
    public Integer getInitialProjectDTOId() {
        return this.initialProjectDTOId;
    }

    public void setInitialProjectDTOId(Integer initialProjectDTOId) {
        this.initialProjectDTOId = initialProjectDTOId;
    }

    @OneToMany(mappedBy = "studentprojects", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
    public Set<StudentProjectDTO> getUserRole() {
        return this.studentProjectDTO;
    }

    public void setUserRole(Set<StudentProjectDTO> studentProjectDTO) {
        this.studentProjectDTO = studentProjectDTO;
    }  

}

@Entity
@Table(name="studentprojects")
public class StudentProjectDTO {

    private InitialProjectDTO project;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "initial_projectDTO_id", nullable = false)
    public User getProject() {
        return this.project;
    }

    public void setProject(InitialProjectDTO project) {
        this.project = project;
    }
}

你的查询应该是这样的

String hql="SELECT ip from InitialProjectDTO ip JOIN ip.studentProjectDTO sp WHERE sp.projectId = :id";
        Query query=session.createQuery(hql);
        query.setParameter("id", id);
        mail =query.list();