hibernate关联映射
2024-08-24 01:19:17
首先,建立user表,news表
建立User,News类
package com.example.hibernate; import java.util.Set; public class User { private int id;
private String name;
private String head_image;
private String mobile;
private String email;
private String address;
private int age;
private String user_no;
private String password;
private Set<News> newsSet; 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 getHead_image() {
return head_image;
}
public void setHead_image(String head_image) {
this.head_image = head_image;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUser_no() {
return user_no;
}
public void setUser_no(String user_no) {
this.user_no = user_no;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Set<News> getNewsSet() {
return newsSet;
}
public void setNewsSet(Set<News> newsSet) {
this.newsSet = newsSet;
} }
package com.example.hibernate; import java.util.Date; public class News { private int id;
private String title;
private User user; public News(String title) {
super();
this.title = title;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
} }
配置映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.example.hibernate.User" table="user" > <id name="id">
<generator class="native"/>
</id> <property name="name" column="name" />
<property name="head_image" column="head_image" />
<property name="mobile" column="mobile" />
<property name="email" column="email" />
<property name="address" column="address" />
<property name="age" column="age" />
<property name="user_no" column="user_no" />
<property name="password" column="password" /> <set name="newsSet" table="news" cascade="save-update,delete">
<key column="user_id"></key>
<one-to-many class="com.example.hibernate.News"/>
</set>
</class> </hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.example.hibernate.News" table="news"> <id name="id">
<generator class="native"/>
</id> <property name="title" column="title" /> <many-to-one name="user" column="user_id" class="com.example.hibernate.User"></many-to-one> </class> </hibernate-mapping>
配置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节点就够了,除非我们中间使用了多个数据库-->
<session-factory>
<!--用户名 -->
<property name="connection.username">root</property>
<!--url信息 -->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernte_test</property>
<!--数据库方言信息-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--密码 -->
<property name="connection.password">1111</property>
<!--数据库驱动信息 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.show_sql">true</property> <!--指定Hibernate映射文件路径 -->
<mapping resource="com/example/hibernate/User.hbm.xml" />
<mapping resource="com/example/hibernate/News.hbm.xml" />
</session-factory>
</hibernate-configuration>
编写测试类:
package com.example.hibernate; import java.util.HashSet; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class MyTest { SessionFactory sessionFactory;
Session session;
Transaction transaction; @Before
public void init(){
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
} @Test
public void save(){
User user = new User();
user.setAddress("福建厦门鼓浪屿");
user.setAge(10);
user.setEmail("1233454543@zzu.cn");
user.setHead_image("ïmg1.jpg");
user.setMobile("123224324");
user.setName("王天才");
user.setPassword("11111");
user.setUser_no("11111");
user.setNewsSet(new HashSet<News>()); News news1 = new News("测试一");
News news2 = new News("测试二"); news1.setUser(user);
news2.setUser(user); user.getNewsSet().add(news1);
user.getNewsSet().add(news2);
//会多执行多余语句,切记需要添加inverse(本程序未添加)
session.save(user);
} @After
public void destroy(){
transaction.commit();
session.close();
sessionFactory.close();
} }
最新文章
- 安装Python算法库
- InventSumDelta表的作用
- Python编写邮件群发(qq邮箱)
- Android设置AlertDialog点击按钮对话框不关闭(转)
- IOS-ARC和垃圾回收机制
- rtsp实时流通过rtmp推送到服务端
- <;Error>;: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
- namenode ha
- FutureTask分析(1.8)
- 使用ZooKeeper协调多台Web Server的定时任务处理(方案1)
- LeetCode算法题-Find All Anagrams in a String(Java实现)
- Echo团队Alpha冲刺随笔 - 第五天
- Django之视图函数总结
- ORM操作 数据库外键 一对多
- GridView Bind 短日期 格式
- OpenvSwitch端口镜像
- [WPF]DropShadowEffect导致Image模糊问题
- CI框架 -- 文件结构
- js时钟
- PHP高级教程-Cookie