[NetBeans] 넷빈즈를 이용한 Hibernate 초간단 예제1 - Hibernate 셋팅
에 이어서-_-;이제 셋팅은 다 된 것 같으니, 이 데이터를 가져와봅시다.
Hibernate에서 쉽게 데이터를 가져올 수 있도록 Helper클래스를 만들어봅시다.
Ctrl + N -> Java -> Java Class -> Class Name은 ScoreHelper, Package는 wondergirlshibernate로 합니다.
ScoreHelper.java
[code]package wondergirlshibernate;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import wondergirlshibernate.entity.Score;
import wondergirlshibernate.util.HibernateUtil;
public class ScoreHelper {
Session session = null;
public ScoreHelper() {
this.session = HibernateUtil.getSessionFactory().openSession();
}
public List<Score> getWondergirlsScore() {
List<Score> wondergirlsScoreList = null;
try {
Transaction tx = session.beginTransaction();
Query q = session.createQuery("from Score");
wondergirlsScoreList = (List<Score>) q.list();
} catch (HibernateException e) {
e.printStackTrace();
}
return wondergirlsScoreList;
}
}
[/code]
getWondergirlsScore를 보면 from Score를 실행하는 것이 다 입니다. 그리고, 그 리스트를 받아오는 거죠.
Main클래스를 봅시다.
Main.java
[code]package wondergirlshibernate;
import java.util.List;
import wondergirlshibernate.entity.Score;
public class Main {
public static void main(String[] args) {
ScoreHelper helper = new ScoreHelper();
List list = helper.getWondergirlsScore();
System.out.println(
String.format("%10s %10s %10s %10s", "이름", "나이", "국어", "수학"));
for (int i = 0; i < list.size(); i++) {
Score score = (Score) list.get(i);
System.out.println(
String.format("%10s %10s %10d %10d",
score.getWondergirls().getName(),
score.getWondergirls().getAge(),
score.getKorean(),
score.getMath())
);
}
}
}
[/code]
그냥 출력합니다-_-; 결과는 이렇게 나올 겁니다.
[code]Hibernate: select score0_.idx as idx1_, score0_.widx as widx1_, score0_.korean as korean1_, score0_.math as math1_ from wondergirls.score score0_
이름 나이 국어 수학
Hibernate: select wondergirl0_.idx as idx0_0_, wondergirl0_.name as name0_0_, wondergirl0_.age as age0_0_ from wondergirls.wondergirls wondergirl0_ where wondergirl0_.idx=?
선예 20 100 100
Hibernate: select wondergirl0_.idx as idx0_0_, wondergirl0_.name as name0_0_, wondergirl0_.age as age0_0_ from wondergirls.wondergirls wondergirl0_ where wondergirl0_.idx=?
선미 20 90 90
Hibernate: select wondergirl0_.idx as idx0_0_, wondergirl0_.name as name0_0_, wondergirl0_.age as age0_0_ from wondergirls.wondergirls wondergirl0_ where wondergirl0_.idx=?
소희 17 80 80
Hibernate: select wondergirl0_.idx as idx0_0_, wondergirl0_.name as name0_0_, wondergirl0_.age as age0_0_ from wondergirls.wondergirls wondergirl0_ where wondergirl0_.idx=?
예은 17 70 70
Hibernate: select wondergirl0_.idx as idx0_0_, wondergirl0_.name as name0_0_, wondergirl0_.age as age0_0_ from wondergirls.wondergirls wondergirl0_ where wondergirl0_.idx=?
유빈 21 60 60
BUILD SUCCESSFUL (total time: 3 seconds)
[/code]
음... 저기서 wondergirls참조하는 것을 가져오려고 하면 다시 쿼리를 날리는 군요-_-; 뭔가 다른 방법이 있는 건가...
Hibernate를 더 공부해봐야겠습니다. 넷빈즈에서 쉽게 Hibernate를 할 수 있네요. 예전에 Hibernate Hello World예제를 따라해보고 셋팅에 겁을 먹은 기억이 있었는데, 쉽게 할 수 있네요.
이제 공부를 해봅시다 ㅠㅠ