مشخصات مقاله
-
1027
-
0.0
-
2211
-
0
-
0
درس 26 : آموزش آدرس دهی مجموعه های Hibernate با xml
آموزش آدرس دهی Map در مجموعه های Hibernate با استفاده از فایل xml :
Hibernate به شما امکان آدرس دهی المنت Map را به وسیله RD MS می دهد. همان طور که می دانید المنت های map و list، ایندکس گذاری شده یا index-basesd هستند. در این موارد، ستون index به عنوان کلید key و ستون element به عنوان مقدار یا Value عمل می کند.
مثال عملی آدرس دهی Map در مجموعه های Hibernate با استفاده از فایل xml :
برای آدرس دهی المنت map بایستی صفحات زیر را در برنامه خود ایجاد کنید :
- Question.java
- question.hbm.xml
- hibernate.cfg.xml
- StoreTest.java
- FetchTest.java
کد هر یک از فایل های فوق به صورت زیر تعیین می شوند :
فایل Question.java
package com.javatpoint;
import java.util.Map;
public class Question {
private int id;
private String name,username;
private Map answers;
public Question() {}
public Question(String name, String username, Map answers) {
super();
this.name = name;
this.username = username;
this.answers = answers;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Map getAnswers() {
return answers;
}
public void setAnswers(Map answers) {
this.answers = answers;
}
}
فایل question.hbm.xml
فایل hibernate.cfg.xml
update org.hibernate.dialect.Oracle9Dialect jdbc:oracle:thin:@localhost:1521:xe system oracle oracle.jdbc.driver.OracleDriver
فایل StoreTest.java
package com.javatpoint;
import java.util.HashMap;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class StoreTest {
public static void main(String[] args) {
Session session=new Configuration().configure().buildSessionFactory().openSession();
Transaction tx=session.beginTransaction();
HashMap map1=new HashMap();
map1.put("java is a programming language","John Milton");
map1.put("java is a platform","Ashok Kumar");
HashMap map2=new HashMap();
map2.put("servlet technology is a server side programming","John Milton");
map2.put("Servlet is an Interface","Ashok Kumar");
map2.put("Servlet is a package","Rahul Kumar");
Question question1=new Question("What is java?","Alok",map1);
Question question2=new Question("What is servlet?","Jai Dixit",map2);
session.persist(question1);
session.persist(question2);
tx.commit();
session.close();
System.out.println("successfully stored");
}
}
فایل FetchTest.java
package com.javatpoint;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class FetchTest {
public static void main(String[] args) {
Session session=new Configuration().configure().buildSessionFactory().openSession();
Query query=session.createQuery("from Question ");
List list=query.list();
Iterator iterator=list.iterator();
while(iterator.hasNext()){
Question question=iterator.next();
System.out.println("question id:"+question.getId());
System.out.println("question name:"+question.getName());
System.out.println("question posted by:"+question.getUsername());
System.out.println("answers.....");
Map map=question.getAnswers();
Set<>> set=map.entrySet();
Iterator<>> iteratoranswer=set.iterator();
while(iteratoranswer.hasNext()){
Map.Entry entry=(Map.Entry)iteratoranswer.next();
System.out.println("answer name:"+entry.getKey());
System.out.println("answer posted by:"+entry.getValue());
}
}
session.close();
}
}