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 (人-地址-学校)的更多相关文章

  1. Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister报错解决办法

    在做Hibernate框架数据库的关联关系映射练习中出现了Could not get constructor for org.hibernate.persister.entity.SingleTabl ...

  2. JDK注解替代Hibernate的Entity映射

    1.在entity(实体类)模块中使用注解 1_1.注解的位置出现在 [类定义的前面] 和 [属性的get方法前面] [属性的get方法前面] Java代码: package app.entity; ...

  3. hibernate中@Entity和@Table的区别

    Java Persistence API定义了一种定义,可以将常规的普通Java对象(有时被称作POJO)映射到数据库.这些普通Java对象被称作Entity Bean.除了是用Java Persis ...

  4. 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 ...

  5. 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 ...

  6. HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTup

    Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.t ...

  7. Hibernate component mapping

    A Component is a containted object that is be persisted value type and not an entity.But you can emb ...

  8. Hibernate中Entity实体类的写法

    记录下一个Entity类的写法,方便以后查阅: package com.bupt.auth.entity; import java.util.Date; import javax.persistenc ...

  9. hibernate在写cfg配置文件自动创建表时报错org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister

    在用hibernate框架时,写cfg文件,想自动生成表时,一般写<property name="hibernate.hbm2ddl.auto">create</ ...

随机推荐

  1. Thrift架构~windows下安装和Hello World及编码引起的错误

    最近开始正式接触Thrift架构,很牛B的技术,它被apache收纳了,属于开源中的一员,呵呵. 概念: Thrift源于大名鼎鼎的facebook之手,在2007年facebook提交Apache基 ...

  2. Atitit 图像处理30大经典算法attilax总结

    Atitit 图像处理30大经典算法attilax总结 1. 识别模糊图片算法2 2. 相似度识别算法(ahash,phash,dhash)2 3. 分辨率太小图片2 4. 横条薯条广告2 5. 图像 ...

  3. Atitit.rsa密钥生成器的attilax总结

    Atitit.rsa密钥生成器的attilax总结 1.1. 密钥生成器 1 1.2. 生成固定的密钥 2 1.2.1. 设置或重置 SecureRandom 对象的随机数种子 2 1.3. 密钥结构 ...

  4. FIR.im Weekly - 这是多产的一周

    前方干货颇多,请备好小板凳,泡好茶,慢慢看. 上周四到周六(23号-25号),由 infoQ 主办的 QCon 在北京召开,各技术大牛围绕敏捷开发.技术架构.开发实践等话题进行了技术分享与探讨. QC ...

  5. mybatis插入的同时获取主键id

    <insert id="insertAndReturnId" parameterType="com.qianlong.cms.entity.AppCmsRole&q ...

  6. v-show和v-if的区别

    v-show和v-if的区别

  7. viewpage listview gridview加载本地大图多图OOM处理办法

    很少上博客园写东西了. 最近在写公司项目,由于需要加载本地相册通过viewpager方式来加载, 最后发现直接进入界面就OOM了. 经过几天的整理最终搞定. 现在将加载本地和加载网络图片的缓存工具类贴 ...

  8. 练习2 练习目标-使用引用类型的成员变量:在本练习中,将扩展银行项目,添加一个(客户类)Customer类。Customer类将包含一个Account对象。

    package banking; public class Customer { private String firstName; private String lastName; private ...

  9. CSS图片裁剪Clip

    CSS Clip 裁剪 可以用它来裁剪一张图片噢. 我们来裁剪一下Google图标,把G字给裁剪下来. 注意这个clip得配合绝对定位才能使用,不然不起效果. rect (top, right, bo ...

  10. WPF Bitmap转Imagesource

    var imgsource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),IntPtr ...