2016-03-25 14 views
1

Ich versuche, im Winterschlaf beizutreten, und ich benutze Struts2. Ich arbeite mit Hibernate mit Annotations. Jetzt bin ich nicht in der Lage, Join zwischen zwei Tabellen durchzuführen. Meine erste Tabelle ist "studentprojects", die pid und email enthalten. Zweite Tabelle ist "initialprojectdetials", die pid, Name, Beschreibung enthält ... ähnlich einigen anderen fields.I muss das bekommen Daten der zweiten Tabelle durch Durchführen von Join-Around-PID der ersten Tabelle. Dazu ist mit dieser Abfrage:Hibernate mit Annotaion

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(); 

wo Mail ist die Arraylist von InitialProjectDTO. Und mein InitialProjectDTO ist:

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",[email protected](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; 
    } 

} 

mein SudentProjectDTO ist:

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; 
    } 

} 

Dies ist der Fehler, die ich erhalte:

Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: edu.pma.dto.InitialProjectDTO.projectId 
Method "execute" failed for object [email protected] 
File: org/hibernate/cfg/annotations/CollectionBinder.java 
+0

Vielen Dank für Ihre Antwort sein. Aber es hat nicht für mich funktioniert. Ich benutze Annotation deshalb kann ich kein zusätzliches Feld in meiner DTO-Klasse hinzufügen außer denen, die die Teil-DB-Tabelle sind. Deshalb gibt es mir den Fehler, die Sesdion-Fabrik nicht zu bauen. –

Antwort

0

Sie sollten versuchen, verschiedene Modelle verwenden

@Entity 
public class InitialProjectDTO { 

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

} 

@Entity 
public class StudentProjectDTO { 

@ManyToOne 
private InitialProjectDTO project; 

} 

Und mit dem richtigen Modell sollte es einfach sein, hql zu schreiben, vielleicht möchten Sie hier Beispiele für https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html suchen.

Auch würde ich vorschlagen, hier zum Beispiel von Modellen zu suchen. http://viralpatel.net/blogs/hibernate-one-to-many-annotation-tutorial/

0

Siehe folgendes Beispiel könnte seine Hilfe für Sie

@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; 
    } 
} 

Query shoud so etwas wie dieser

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();