hibernate----1-1-----两表关联属性放在另一个表里面

package com.ij34.dao;

import javax.persistence.*;

@Entity
@Table(name="Address_inf")
public class Address{
    @Id @Column(name="address_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int addressId;
    private String message;
    @OneToOne(targetEntity=People.class)

    @JoinTable(name="people_address"
    ,joinColumns[email protected](name="addressId" ,referencedColumnName="address_id" ,unique=true)
    ,inverseJoinColumns[email protected](name="peopleId" ,referencedColumnName="people_id" ,unique=true)
    )
    private People people;
    public int getAddressId() {
        return addressId;
    }
    public void setAddressId(int addressId) {
        this.addressId = addressId;
    }
    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;
    }

}

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;
	@OneToOne(targetEntity=Address.class)
	@JoinTable(name="people_address"
	,[email protected](name="peopleId" ,referencedColumnName="people_id" ,unique=true)
	,[email protected](name="addressId" ,referencedColumnName="address_id" ,unique=true)
	)
	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.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;
    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("林彪");
   Address a=new Address();
   a.setMessage("广州");
   a.setPeople(people);
   session.persist(a);
   session.save(people);
    tx.commit();
    session.close();
    SF.close();
    }
    }

十月 16, 2016 11:11:12 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
十月 16, 2016 11:11:12 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
十月 16, 2016 11:11:12 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十月 16, 2016 11:11:12 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十月 16, 2016 11:11:12 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
十月 16, 2016 11:11:12 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
十月 16, 2016 11:11:13 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
十月 16, 2016 11:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
十月 16, 2016 11:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate]
十月 16, 2016 11:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
十月 16, 2016 11:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
十月 16, 2016 11:11:13 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Sun Oct 16 23:11:13 CST 2016 WARN: Establishing SSL connection without server‘s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn‘t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false‘. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
十月 16, 2016 11:11:13 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
十月 16, 2016 11:11:13 下午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
十月 16, 2016 11:11:13 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.address_inf
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [address_id, message]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.people_address
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [peopleid, addressid]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: [fk_16h1wo9lfnwknsfcywssb636a, fk_aue0yqtcmxoo9hxc2cax44jg4]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [fk_16h1wo9lfnwknsfcywssb636a, primary]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.people_inf
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [people_id, name, age]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
十月 16, 2016 11:11:14 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: insert into Address_inf (message) values (?)
Hibernate: insert into people_inf (age, name) values (?, ?)
Hibernate: insert into people_address (peopleId, addressId) values (?, ?)
十月 16, 2016 11:11:14 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate]

时间: 2024-08-06 16:06:12

hibernate----1-1-----两表关联属性放在另一个表里面的相关文章

MySQL 两张表关联更新(用一个表的数据更新另一个表的数据)

有两张表,info1, info2 . info1: info2: 现在,要用info2中的数据更新info1中对应的学生信息,sql语句如下: UPDATE info1 t1 JOIN info2 t2 ON t1.name = t2.name SET t1.age = t2.age, t1.class = t2.class; 运行结果如下: 更新过的info1: 至于效率问题,之前我有三张表,都在40万左右.需要将 table2 中的两个字段(step1),table3 中的一个字段(ste

Oracle两张表关联批量更新其中一张表的数据

Oracle两张表关联批量更新其中一张表的数据 方法一(推荐): UPDATE 表2 SET 表2.C = (SELECT B FROM 表1 WHERE 表1.A = 表2.A) WHERE EXISTS (SELECT 1 FROM 表1 WHERE 表1.A = 表2.A); 尤其注意最后的外层where条件尤为重要,是锁定其批量更新数据的范围. 方法二: MERGE INTO 表2 USING 表1 ON (表2.A = 表1.A) -- 条件是 A 相同 WHEN MATCHED TH

SQL两表之间:根据一个表的字段更新另一个表的字段

1. 写法轻松,更新效率高: update table1 set field1=table2.field1, field2=table2.field2 from table2 where table1.id=table2.id 2. 常规方式,种写法相当于一个 Left join, 以外面的where为更新条数,如果不加where就是所有记录 update table1 set field1=(select top 1 field1 from table2 where table2.id=tab

三张表关联 修改第三张表所关联前两张表的某个字段的值

---三张表关联 利用关联的字段 表一关联表二 表二关联表三 来修改表三对应字段下的某个值. update GY_FYJY set zfbl='50' where fyxh in (select fyxh from gy_ylsf where fygb ='26')

20亿与20亿表关联优化方法(超级大表与超级大表join优化方法)

记得5年前遇到一个SQL.就是一个简单的两表关联.SQL跑了几乎相同一天一夜,这两个表都非常巨大.每一个表都有几十个G.数据量每一个表有20多亿,表的字段也特别多. 相信大家也知道SQL慢在哪里了,单个进程的PGA 是绝对放不下几十个G的数据,这就会导致消耗大量temp tablespace,SQL慢就是慢在temp来回来回来回...的读写数据. 遇到这样的超级大表与超级大表怎么优化呢?这篇文章将告诉你答案. 首先创建2个測试表 t1,t2 数据来自dba_objects create tabl

salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解

建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema Builder查看表结构以及多表之间的关联关系,可以登录后点击setup在左侧搜索框输入schema Builder 或者build-->schema Builder进入: 2.采用force.com Explorer通过自己写查询语句来查询数据. 此链接为force.com Explorer的下载链接:  http://force-com-explorer-beta.software.infor

jquery过滤选择器-----------(表单对象属性过滤选择器 与 表单选择器)

1.表单对象属性选择器 2.程序 3.表单选择器

delete表1条件是另一个表中的数据,多表连接删除(转)

DELETE删除多表数据,怎样才能同时删除多个关联表的数据呢?这里做了深入的解释:1. delete from t1 where 条件2.delete t1 from t1 where 条件3. delete t1 from t1,t2 where 条件4.delete t1,t2 from t1,t2 where 条件前 3者是可行的,第4者不可行.也就是简单用delete语句无法进行多表删除数据操作,不过可以建立级联删除,在两个表之间建立级联删除关系,则可以实现删除一个表的数据时,同时删除另

用PL/SQL DEVELOPER把表数据复制到另一个表中

1.创建新表,business_copy,并复制bisiness表的数据 CREATE TABLE BUSINESS_COPY AS SELECT * FROM BUSINESS; 注释:此SQL语句只是创建了表和复制了表的数据,但是没有把表的约束复制过来. 2.在PL/SQL DEVELOPER删除表 例如删除表business,可执行一下操作, 1)DROP TABLE BUSINESS; 2)找到此表,单击右键,选择跨界删除操作. 3.在PL/SQL DEVELOPER快速创建表. 4.如