Hibernate - Criteria interface pytanie

0

Witam.
Mam 2 klasy(Exam + ExamDetail) ktore mapuje do 2 tablic uzywajac JPA Annotation OneToOne. Przy anotacji OneToOne uzylem CascadeType.ALL aby latwiej mi bylo zachowac rzeczy.

Teraz, chcialbym uzyc interfejsu Criteria aby wyciagnac te Examiny, w ktorych ExamDetail percentage jest wiekszy od jakies tam wartosci.

Code so far:

Exam.java

package model2;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;


@Entity
public class Exam 
{
    private long id;
    private String shortName;
    private ExamDetail detail;
    
    public Exam(){}
    
    public Exam(String shortName, ExamDetail detail){
        this.shortName = shortName;
        this.detail = detail;
    }
    
    @Id
    @GeneratedValue
    public long getId(){return id;}
    public String getShortName(){return shortName;}
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "detail_id")
    public ExamDetail getDetail(){return detail;}
    
    public void setId(long id){this.id = id;}
    public void setShortName(String s){shortName = s;}
    public void setDetail(ExamDetail ed){detail = ed;}
    
}

 

ExamDetail.java

 
package model2;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity
public class ExamDetail 
{
    @Id
    @GeneratedValue
    private long id;
    private String fullName;
    private int numberOfQuestions;
    private int passingPercentage;

    
    public ExamDetail(){}
    
    public ExamDetail(String fullName, int questions, int percentage){
        this.fullName = fullName;
        this.numberOfQuestions = questions;
        this.passingPercentage = percentage;
    }
    
    
    public long getId() {
        return id;
    }

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

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public int getNumberOfQuestions() {
        return numberOfQuestions;
    }

    public void setNumberOfQuestions(int numberOfQuestions) {
        this.numberOfQuestions = numberOfQuestions;
    }

    public int getPassingPercentage() {
        return passingPercentage;
    }

    public void setPassingPercentage(int passingPercentage) {
        this.passingPercentage = passingPercentage;
    }
    
}

i proba metody retrieve()

public static void retrieve(int number){
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Exam.class);
        config.addAnnotatedClass(ExamDetail.class);
        config.configure();
        SessionFactory factory = config.buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Exam.class);
        //get all EXAMS where EXAMDETAIL percentage >= number
        session.getTransaction().commit();
    }
 

Chcialbym wydobyc wszystkie Examy gdzie percentage jest wiekszy lub rowny liczbie w parametrze.
Jak moglibyscie mnie popchnac w dobra strone byloby extra.

Senk ju.

0

Restrictions i tutaj masz statyczne metody. Będzie to albo metoda gt albo greatherThan. Już nie pamiętam.

Używanie kaskady ALL z lenistwa to totalna głupota. Jeśli chcesz tylko zapisywać to użyj kaskady PERSIST.
Chociaż z drugiej strony widzę, że encja ExamDetails jest prywatna dla Exam, więc jeszcze ALL jest usprawiedliwione :-)

1 użytkowników online, w tym zalogowanych: 0, gości: 1