加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

映射Map(包含对象)(参考张龙老师的)

发布时间:2020-12-13 17:49:42 所属栏目:百科 来源:网络整理
导读:1.Team.java package org.yang.hibernate; import java.util.HashMap; import java.util.Map; public class Team { private String id; private String teamName; private MapString,Student students = new HashMapString,Student(); public String getId()

1.Team.java

package org.yang.hibernate;


import java.util.HashMap;
import java.util.Map;

public class Team
{
private String id;
private String teamName;
private Map<String,Student> students = new HashMap<String,Student>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public Map<String,Student> getStudents() {
return students;
}
public void setStudents(Map<String,Student> students) {
this.students = students;
}
}


2.Student.java:

package org.yang.hibernate;

public class Student
{
private String id;
private String cardId;
private String name;
private int age;

private Team team;

public String getId() {
return id;
}

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

public String getCardId() {
return cardId;
}

public void setCardId(String cardId) {
this.cardId = cardId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Team getTeam() {
return team;
}

public void setTeam(Team team) {
this.team = team;
}


}

3.Team.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="org.yang.hibernate.Team" table="team">
<id name="id" column="id" type="string">
<generator class="uuid"></generator>
</id>

<property name="teamName" type="string">
<column name="teamname" length="13"></column>
</property>

<map name="students" table="student" cascade="all">
<key column="team_id"></key>
<!-- 这里的index存放的是KEY -->
<index column="card_id" type="java.lang.String"></index>

<one-to-many class="org.yang.hibernate.Student"/>
</map>

</class>
</hibernate-mapping>

4.Student.hbm.xml;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="org.yang.hibernate.Student" table="student">
<id name="id" column="id" type="string">
<generator class="uuid"></generator>
</id>

<property name="cardId" column="card_id" type="string"></property>
<property name="name" column="name" type="string"></property>
<property name="age" column="age" type="integer"></property>

<many-to-one name="team" column="team_id" class="org.yang.hibernate.Team" fetch="join"></many-to-one>

</class>
</hibernate-mapping>

5.hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/myhibernate</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">yang</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>

<mapping resource="Team.hbm.xml" />
<mapping resource="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>


6.CreateTable.java

package org.yang.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class CreateTable
{
public static void main(String[] args)
{
SchemaExport export = new SchemaExport(new Configuration().configure());
export.create(true,true);
}
}


7.测试文件:

package org.yang.hibernate;

import java.util.Map;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test
{
private static SessionFactory sessionFactory;
static
{
try
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}

public static void main(String[] args)
{
Session session = sessionFactory.openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
Team team = new Team();

Student student = new Student();
student.setAge(20);
student.setName("zhangsan");
student.setTeam(team);

Student student2 = new Student();
student2.setAge(20);
student2.setName("zhangsan2");
student2.setTeam(team);

Student student3 = new Student();
student3.setAge(20);
student3.setName("zhangsan3");
student3.setTeam(team);

team.setTeamName("team1");

Map<String,Student> map = team.getStudents();
map.put("111",student);
map.put("222",student2);
map.put("333",student3);

session.save(team);
tx.commit();
}
catch(Exception ex)
{
if (null != tx)
{
tx.rollback();
}

ex.printStackTrace();
}
finally
{
session.close();
}

}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读