hibernate----component-entity (人-地址-学校)
package com.ij34.dao; import javax.persistence.*; @Entity
@Table(name="school_inf")
public class School {
@Id @Column(name="school_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
package com.ij34.dao; import javax.persistence.*; @Entity
@Table(name="people_inf")
public class People implements java.io.Serializable{
private static final long serialVersionUID = 1L;
@Id @Column(name="people_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String name;
private int age; private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
} }
package com.ij34.dao; import java.util.HashSet;
import java.util.Set; import javax.persistence.*; import org.hibernate.annotations.Parent; @Embeddable
public class Address{
@Column(name="message")
private String message;
@Parent
private People people;
@OneToMany(targetEntity=School.class)
@JoinColumn(name="address_id",referencedColumnName="people_id")
private Set<School> school=new HashSet<>();
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public People getPeople() {
return people;
}
public void setPeople(People people) {
this.people = people;
}
public Set<School> getSchool() {
return school;
}
public void setSchool(Set<School> school) {
this.school = school;
} }
package com.ij34.web; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.*; import com.ij34.dao.Address;
import com.ij34.dao.People;
import com.ij34.dao.School;
public class test01 {
public static void main(String[] args)throws Exception {
//实例化Configuration
Configuration conf=new Configuration().configure();
ServiceRegistry SR=new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
// 以Configuration实例创建SessionFactory实例
SessionFactory SF=conf.buildSessionFactory(SR);
//create session
Session session=SF.openSession();
//start 事务
Transaction tx=session.beginTransaction();
People person = new People();
person.setAge(29);
// 为复合主键的两个成员设置值
People people=new People();
people.setAge(22);
people.setName("林彪");
// 持久化People对象(对应于插入主表记录)
session.save(people);
Address a=new Address();
a.setMessage("广州");
people.setAddress(a);
School s1=new School();
School s2=new School();
s1.setName("哈佛大学");
s2.setName("社会大学");
session.save(s1);
session.save(s2);
a.getSchool().add(s2);
a.getSchool().add(s1);
tx.commit();
session.close();
SF.close();
}
}
hibernate----component-entity (人-地址-学校)的更多相关文章
- Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister报错解决办法
在做Hibernate框架数据库的关联关系映射练习中出现了Could not get constructor for org.hibernate.persister.entity.SingleTabl ...
- JDK注解替代Hibernate的Entity映射
1.在entity(实体类)模块中使用注解 1_1.注解的位置出现在 [类定义的前面] 和 [属性的get方法前面] [属性的get方法前面] Java代码: package app.entity; ...
- hibernate中@Entity和@Table的区别
Java Persistence API定义了一种定义,可以将常规的普通Java对象(有时被称作POJO)映射到数据库.这些普通Java对象被称作Entity Bean.除了是用Java Persis ...
- hibernate延迟加载org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.javakc.hibernate.onetomany.entity.DeptEntity.emp, could not initialize proxy - no Session
public static void main(String[] args) { DeptEntity dept = getDept("402882e762ae888d0162ae888e ...
- org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
使用Hibernate 插入List数据时出现了以下异常: SLF4J: The requested version 1.6 by your slf4j binding is not compatib ...
- HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTup
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.t ...
- Hibernate component mapping
A Component is a containted object that is be persisted value type and not an entity.But you can emb ...
- Hibernate中Entity实体类的写法
记录下一个Entity类的写法,方便以后查阅: package com.bupt.auth.entity; import java.util.Date; import javax.persistenc ...
- hibernate在写cfg配置文件自动创建表时报错org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
在用hibernate框架时,写cfg文件,想自动生成表时,一般写<property name="hibernate.hbm2ddl.auto">create</ ...
随机推荐
- CGFloat Float 互转
直接上代码吧 var positionX:CGFloat = 10 var positionY:CGFloat = 20 var tmpX:Float = 30 var tmpY:Float = 40 ...
- NodeJS系列~第三个小例子,NodeJs与Redis实现高并发的队列存储
返回目录 众所周知 redis量个强大的缓存组件,可以部署在win32和linux环境之上,它有五大存储结构,其中有一种为列表list,它可以实现quene和stack的功能,即队列和堆栈的功能. r ...
- Atitit 图像处理 平滑 也称 模糊, 归一化块滤波、高斯滤波、中值滤波、双边滤波)
Atitit 图像处理 平滑 也称 模糊, 归一化块滤波.高斯滤波.中值滤波.双边滤波) 是一项简单且使用频率很高的图像处理方法 用途 去噪 去雾 各种线性滤波器对图像进行平滑处理,相关OpenC ...
- fir.im Weekly - 2015 年开发者调查报告
终于一脚迈入了 2016 年.无论你是否准备好,未来已经汹涌扑来-- 新年第一期的 fir.im Weekly 干货颇多,来看一看:) 2015 Developer Survey stackoverf ...
- fir.im Weekly - 给女朋友的 iOS 开发教程
俗话说:技多不压身,功到自然成.本期 fir.im Weekly 收集的热度资源,大部分关于Android.iOS 开发工具和源码,还有一些有关设计的 Tips ,希望对你有帮助. 给女朋友的 iOS ...
- linux配置hosts
linux配置hosts linux下配置hosts和windows下其实就是一样的,找到文件在哪里就好 sudo vim /etc/hosts
- How Google TestsSoftware - Crawl, walk, run.
One of the key ways Google achievesgood results with fewer testers than many companies is that we ra ...
- 两套JRE
JDK就是Java Development Kit.简单的说JDK是面向开发人员使用的SDK,它提供了Java的开发环境和运行环境. SDK是Software Development Kit 一般指软 ...
- 移动端 几个css3属性的练习
转行做前端,上班有一个星期了,什么都不懂,今天学习了几个新的css3属性,记录下来. 注:所有的测试均是在chrome上手机模式测试,与真实的手机环境可能有误差 1:-webkit-tap-heigh ...
- 使用模板技术处理ASP.NET中GridView额外序号列的问题
问题描述: 现在要在一张GridView表中添加一列来显示行号,要求是显示下一页的时候能够递增而不是从新编号.数据库中的没有相关序号列 如何在软件端实现呢? 通过测试,添加以下代码即可解决需求. &l ...