Hibernate 설정 파일 기반 (15) 집합 맵 List, Map
25020 단어 Hibernate
관련 맵 으로 one - to - many many - to - one 등급
대상 은 하나의 실체 클래스 가 아 닙 니 다. 예 를 들 어 String, Integer. Address (id 필드 없 음) 등 은 집합 으로 저장 합 니 다. 집합 맵 ListSetMap 을 삭제 할 때 해당 표 의 모든 데 이 터 를 자동 으로 삭제 합 니 다 (직렬 삭제 와 유사).
===========================================================================
집합 맵 설정 (중복 되 지 않 음)
package org.leadfar.hibernate.model;
import java.util.HashSet;
import java.util.Set;
public class Person {
private int id;
private String name;
private Set<String> qqNumbers;// QQ,
Person() {
}
public void addQq(String qq){
if(qqNumbers == null) {
qqNumbers = new HashSet<String>();
}
qqNumbers.add(qq);
}
public Set<String> getQqNumbers() {
return qqNumbers;
}
public void setQqNumbers(Set<String> qqNumbers) {
this.qqNumbers = qqNumbers;
}
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;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.auction">
<!-- name table lazy true select , ( id )-->
<class name="org.leadfar.hibernate.model.Person" table="t_person" >
<!-- id , -->
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="qqNumbers" table="t_person_qq">
<key column="personId"></key>
<element type="string" column="qqNumber"></element>
</set>
</class>
</hibernate-mapping>
package org.leadfar.hibernate.model;
import java.util.Set;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test_Component_01 extends TestCase {
public void testSave_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = new Person();
p.setName(" ");
p.addQq("1240234098");
p.addQq("124234");
p.addQq("1234324");
session.save(p);
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
public void testLoad_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = (Person)session.load(Person.class, 1);
System.out.println(p.getName());
Set<String> qqNumbers = p.getQqNumbers();
for(String qq : qqNumbers) {
System.out.println(qq);
}
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
public void testDelete_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = (Person)session.load(Person.class, 1);
session.delete(p);// t_person_qq
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
}
===========================================================================
List 집합 맵 (list 집합 은 색인 번호 가 있어 야 하기 때문에 실체 류 를 저장 하면 한 끝 에서 관련 관 계 를 유지 해 야 하기 때문에 실체 류 맵 에 있어 set 집합 을 사용 하 는 것 이 많 습 니 다!)
package org.leadfar.hibernate.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class Person {
private int id;
private String name;
private List<Integer> qqNumbers;// QQ,
Person() {
}
public void addQq(Integer qq){
if(qqNumbers == null) {
qqNumbers = new ArrayList<Integer>();
}
qqNumbers.add(qq);
}
public List<Integer> getQqNumbers() {
return qqNumbers;
}
public void setQqNumbers(List<Integer> qqNumbers) {
this.qqNumbers = qqNumbers;
}
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;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.auction">
<!-- name table lazy true select , ( id )-->
<class name="org.leadfar.hibernate.model.Person" table="t_person" >
<!-- id , -->
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<list name="qqNumbers" table="t_person_qq">
<key column="personId"></key>
<!-- list -->
<list-index column="qq_index"></list-index>
<element type="int" column="qqNumber"></element>
</list>
</class>
</hibernate-mapping>
package org.leadfar.hibernate.model;
import java.util.List;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test_Component_01 extends TestCase {
public void testSave_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = new Person();
p.setName(" ");
p.addQq(240234098);
p.addQq(124234);
p.addQq(1234324);
session.save(p);
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
public void testLoad_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = (Person)session.load(Person.class, 1);
System.out.println(p.getName());
List<Integer> qqNumbers = p.getQqNumbers();
for(Integer qq : qqNumbers) {
System.out.println(qq);
}
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
public void testDelete_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = (Person)session.load(Person.class, 1);
session.delete(p);// t_person_qq
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
}
===========================================================================
맵 집합 맵
package org.leadfar.hibernate.model;
import java.util.HashMap;
import java.util.Map;
public class Person {
private int id;
private String name;
private Map<String,String> addresses;// QQ,
Person() {
}
public void addAddress(String type,String addr) {
if(addresses==null) {
addresses = new HashMap<String,String>();
}
addresses.put(type,addr);
}
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 void setAddresses(Map<String, String> addresses) {
this.addresses = addresses;
}
public Map<String, String> getAddresses() {
return addresses;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.auction">
<!-- name table lazy true select , ( id )-->
<class name="org.leadfar.hibernate.model.Person" table="t_person" >
<!-- id , -->
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<map name="addresses" table="t_person_address">
<key column="personId"></key>
<map-key type="string" column="addrType"></map-key>
<element type="string" column="addr"></element>
</map>
</class>
</hibernate-mapping>
package org.leadfar.hibernate.model;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test_Component_01 extends TestCase {
public void testSave_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = new Person();
p.setName(" ");
p.addAddress(" 1", "a");
p.addAddress(" 2", "b");
p.addAddress(" 3", "c");
session.save(p);
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
public void testLoad_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = (Person)session.load(Person.class, 1);
System.out.println(p.getName());
Map<String,String> addresses = p.getAddresses();
Set<Map.Entry<String,String>> set = addresses.entrySet();
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
Entry<String, String> entry = (Entry<String, String>) iterator.next();
System.out.println(entry.getKey()+"---"+entry.getValue());
}
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
public void testDelete_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = (Person)session.load(Person.class, 1);
session.delete(p);// t_person_qq
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
}
===========================================================================
package org.leadfar.hibernate.model;
import java.util.HashMap;
import java.util.Map;
public class Person {
private int id;
private String name;
private Map<String,Address> addresses;// Address
Person() {
}
public void addAddress(String type,String province,String city,String street) {
if(addresses==null) {
addresses = new HashMap<String,Address>();
}
Address address = new Address();
address.setProvince(province);
address.setCity(city);
address.setStreet(street);
addresses.put(type,address);
}
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 Map<String, Address> getAddresses() {
return addresses;
}
public void setAddresses(Map<String, Address> addresses) {
this.addresses = addresses;
}
}
package org.leadfar.hibernate.model;
public class Address {
// id
private String province;
private String city;
private String street;
Address(){}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.auction">
<!-- name table lazy true select , ( id )-->
<class name="org.leadfar.hibernate.model.Person" table="t_person" >
<!-- id , -->
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<!-- map , key value personId -->
<map name="addresses" table="t_person_address">
<!-- key t_person id
( t_person_address personId t_person id) -->
<key column="personId"></key>
<!-- map key -->
<map-key type="string" column="addrType"></map-key>
<!-- map value , composite-element -->
<composite-element class="org.leadfar.hibernate.model.Address">
<property name="province"></property>
<property name="city"></property>
<property name="street"></property>
</composite-element>
</map>
</class>
</hibernate-mapping>
package org.leadfar.hibernate.model;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test_Component_01 extends TestCase {
public void testSave_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = new Person();
p.setName(" ");
p.addAddress(" 1", "a","b","c");
p.addAddress(" 2", "d","e","f");
p.addAddress(" 3", "c","h","i");
session.save(p);
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
public void testLoad_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = (Person)session.load(Person.class, 1);
System.out.println(p.getName());
Map<String,Address> map = p.getAddresses();
Set<Map.Entry<String,Address>> set = map.entrySet();
Iterator<Map.Entry<String,Address>> it = set.iterator();
while(it.hasNext()){
Map.Entry<String, Address> entry = it.next();
String name = entry.getKey();
Address addr = entry.getValue();
System.out.println(name+","+addr.getProvince()+","+addr.getCity()+","+addr.getStreet());
}
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
public void testDelete_01() throws Exception {
// hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
// SessionFactory
//buildSessionFactory
SessionFactory sfactory = cfg.buildSessionFactory();
// Hibernate Session
Session session = sfactory.openSession();
try {
//
session.beginTransaction();
Person p = (Person)session.load(Person.class, 1);
session.delete(p);// t_person_qq
//
session.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
// ,
session.getTransaction().rollback();
} finally {
// session
session.close();//session ,user Detached
}
}
}
===========================================================================
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[JPA] 즉시로딩(EAGER)과 지연로딩(LAZY) (왜 LAZY 로딩을 써야할까?) (1)Proxy는 이 글의 주제인 즉시로딩과 지연로딩을 구현하는데 중요한 개념인데, 일단 원리는 미뤄두고 즉시로딩과 지연로딩이 무엇인지에 대해 먼저 알아보자. 눈 여겨 볼 곳은 'fetch = FetchType.EAGER...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.