首页 > 其他分享 >Hibernate annotation 联合主键

Hibernate annotation 联合主键

时间:2023-02-19 11:02:59浏览次数:55  
标签:Hibernate name wife annotation private public import 主键 id


Hibernate annotation 联合主键

 

 

package com.hibernate.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Husband implements Serializable {


/**
*
*/
private static final long serialVersionUID = 2476364405175138004L;


private int id;

private String name;

private Wife wife;


@Id
@GeneratedValue
public int getId() {
return id;
}

public String getName() {
return name;
}

@OneToOne
public Wife getWife() {
return wife;
}

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

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

public void setWife(Wife wife) {
this.wife = wife;
}


}

 

package com.hibernate.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;

@Entity
@IdClass(WifePK.class)//这种设置主键的方式最简单(在下面联合主键的键上设@Id)
public class Wife implements Serializable {

/**
*
*/
private static final long serialVersionUID = 5270130768836331730L;

private int id;

private String name;

@Id
public int getId() {
return id;
}

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

@Id
public String getName() {
return name;
}

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

}

 

Wife的主键类

package com.hibernate.entity;

import java.io.Serializable;


public class WifePK implements Serializable {

/**
*
*/
private static final long serialVersionUID = 5270130768836331730L;


private int id;

private String name;

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;
}



}

 

 

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">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>



<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<property name="format_sql">true</property>
<mapping class="com.hibernate.entity.Husband" />
<mapping class="com.hibernate.entity.Wife" />
</session-factory>

</hibernate-configuration>

 

 

然后用junit4调试:

package com.hibernate.test;


import org.hibernate.SessionFactory;

public class ORMappingTest {

private static SessionFactory sessionFactory;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
sessionFactory.close();
}

@Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
}

}

生成的SQL文日志:

create table Husband (
id integer not null auto_increment,
name varchar(255),
wife_id integer,
wife_name varchar(255),
primary key (id)
)

create table Wife (
id integer not null,
name varchar(255) not null,
primary key (id, name)
)

alter table Husband
add index FKAEEA401B2DB1BA15 (wife_id, wife_name),
add constraint FKAEEA401B2DB1BA15
foreign key (wife_id, wife_name)
references Wife (id, name)

 


标签:Hibernate,name,wife,annotation,private,public,import,主键,id
From: https://blog.51cto.com/u_21817/6066623

相关文章

  • 用myeclipse的Hibernate 反向引擎 生成 数据库的 entity
    把Myeclipse转到DBBrowser 新建一个DatabaseConnectiondriver 然后找到要反向工程的表 右键->HibernateReverseEngineering然后,选好entity缩放的目录对CreatePO......
  • Hibernate 性能优化_1
    大概如此:不一定说在每个项目中都合适 1、比如,开了N多文件而没关,比如开了地址池而没清,比如分页读了N多页而没有清内存 2、对于ManyToOne,如果设为FetchType=Eager,则会产生1+......
  • Hibernate 的 join
    转的: 1.如果没有在Hibernate配置里做关联,在Hql里面是不能用join的。2.Hql里面的join是没有"ON"子句的。3.如果是innerjoin,Join的依据就是事先设计好关联4.如果要用......
  • Hibernate 性能优化_3
    二级缓存 对于二级缓存,其实并不一定要在项目中使用除非是对项目要求非常高的情况下使用 如果要用,应使用在:经常被访问,改动不大,数量不多,比如权限,比如组织机构 load()默认使......
  • Hibernate 性能优化_2
    createQuery("FROM****").list()和createQuery("FROM****").iterate()的区别 1、list()时,会取出所有的数据,Iterate()时,只取所有记录的主键,当用到哪条时,再根据id去取哪条......
  • Spring AOP annotation 简单实例
    最后输出(可以看出各种通知的时间): 我是前置通知。我是环绕--进。aa已成功保存我是后置通知。我是最终通知。我是环绕--出。 app.xml:<?xmlversion="1.0"encoding......
  • 主键、唯一索引、聚集索引、非聚集索引,你真知道他们的区别么?
    【讲故事】近端时间一直在做一些数据库查询的工作,主要是根据表中的“日期”与“产品名”两个字段为条件在对表进行相关查询。但当表数据量达到3000万以上时,发现查询速度......
  • 【MySQL-SQL优化:主键,order by,group by,limit,count,update】
    零、本文纲要一、插入数据二、主键优化三、orderby优化四、groupby优化五、limit优化六、count优化七、update优化update优化tips:Ctrl+F快速定位到所需内容阅读吧。一、......
  • 简单试用Android Annotations
    AndroidAnnotations号称"是一个能够加速Android开发的开源框架,它可以帮助开发者处理一些前后台任务、rest服务、应用类、代码片段等,让开发者专......
  • Annotation - 元注解
     元注解包含@Target,@Retention,@Documented,@Inherited 具体的含义是:@Target表示该注解用于什么地方,可能的ElemenetType参数包括:*ElemenetType.CONST......