hibernate注释映射

Today we will look into One to One Mapping in Hibernate. We will look into Hibernate One To One Mapping example using Annotation and XML configuration.

今天,我们将研究Hibernate中的一对一映射。 我们将研究使用注释和XML配置的Hibernate一对一映射示例。

Hibernate中的一对一映射 (One to One Mapping in Hibernate)

Most of the times, database tables are associated with each other. There are many forms of association – one-to-one, one-to-many and many-to-many are at the broad level. These can be further divided into unidirectional and bidirectional mappings. Today we will look into implementing Hibernate One to One Mapping using XML configuration as well as using annotation configuration.

大多数情况下,数据库表是相互关联的。 关联的形式多种多样- 一对一一对多多对多是广泛的关联。 这些可以进一步分为单向和双向映射。 今天,我们将研究使用XML配置以及注释配置 实现Hibernate一对一映射

Hibernate一对一映射示例数据库设置 (Hibernate One to One Mapping Example Database Setup)

First of all we would need to setup One to One mapping in database tables. We will create two tables for our example – Transaction and Customer. Both of these tables will have one to one mapping. Transaction will be the primary table and we will be using Foreign Key in Customer table for one-to-one mapping.

首先,我们需要在数据库表中设置一对一映射。 我们将为示例创建两个表-交易和客户。 这两个表将具有一对一的映射。 交易将是主要表,我们将使用客户表中的外键进行一对一映射。

I am providing MySQL script, that is the database I am using for this tutorial. If you are using any other database, make sure to change the script accordingly.

我正在提供MySQL脚本,也就是我用于本教程的数据库。 如果使用任何其他数据库,请确保相应地更改脚本。

-- Create Transaction Table
CREATE TABLE `Transaction` (`txn_id` int(11) unsigned NOT NULL AUTO_INCREMENT,`txn_date` date NOT NULL,`txn_total` decimal(10,0) NOT NULL,PRIMARY KEY (`txn_id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;-- Create Customer table
CREATE TABLE `Customer` (`txn_id` int(11) unsigned NOT NULL,`cust_name` varchar(20) NOT NULL DEFAULT '',`cust_email` varchar(20) DEFAULT NULL,`cust_address` varchar(50) NOT NULL DEFAULT '',PRIMARY KEY (`txn_id`),CONSTRAINT `customer_ibfk_1` FOREIGN KEY (`txn_id`) REFERENCES `Transaction` (`txn_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Entity Relation Diagram (ERD) of above one-to-one mapping between tables looks like below image.

表之间一对一映射的实体关系图(ERD)如下图所示。

Our database setup is ready, let’s move on the Hibernate One to One Example Project now.

我们的数据库设置已准备就绪,现在让我们继续进行Hibernate一对一示例项目。

Hibernate一对一映射示例项目结构 (Hibernate One to One Mapping Example Project Structure)

Create a simple Maven project in your Java IDE, I am using Eclipse. Our final project structure will look like below image.

我正在使用Eclipse在Java IDE中创建一个简单的Maven项目。 我们的最终项目结构将如下图所示。

First of all we will look into XML Based Hibernate One to One Mapping example and then we will implement the same thing using annotation.

首先,我们将研究基于XML的Hibernate一对一映射示例,然后将使用批注实现相同的事情。

HibernateMaven依赖关系 (Hibernate Maven Dependencies)

Our final pom.xml file looks like below.

我们最终的pom.xml文件如下所示。

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.journaldev.hibernate</groupId><artifactId>HibernateOneToOneMapping</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>4.3.5.Final</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.0.5</version></dependency></dependencies>
</project>

Dependencies are just for hibernate and mysql java driver. Note that I am using Hibernate latest version 4.3.5.Final and MySQL java driver based on my MySQL database server version (5.0.5).

依赖关系仅适用于Hibernate和mysql Java驱动程序。 请注意,我正在使用基于我MySQL数据库服务器版本(5.0.5)的Hibernate最新版本4.3.5.Final和MySQL Java驱动程序。

Hibernate 4 uses JBoss logging and it gets imported automatically as transitive dependency. You can confirm it in the maven dependencies of the project. If you are using Hibernate older versions, you might have to add slf4j dependencies.

Hibernate 4使用JBoss日志记录,并自动将其作为传递依赖项导入。 您可以在项目的Maven依赖项中进行确认。 如果您使用的是较早版本的Hibernate,则可能必须添加slf4j依赖项。

Hibernate一对一映射模型类 (Hibernate One to One Mapping Model Classes)

Model classes for Hibernate One to One mapping to reflect database tables would be like below.

Hibernate一对一映射以反映数据库表的模型类如下所示。

package com.journaldev.hibernate.model;import java.util.Date;public class Txn {private long id;private Date date;private double total;private Customer customer;@Overridepublic String toString(){return id+", "+total+", "+customer.getName()+", "+customer.getEmail()+", "+customer.getAddress();}public long getId() {return id;}public void setId(long id) {this.id = id;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}public double getTotal() {return total;}public void setTotal(double total) {this.total = total;}public Customer getCustomer() {return customer;}public void setCustomer(Customer customer) {this.customer = customer;}}
package com.journaldev.hibernate.model;public class Customer {private long id;private String name;private String email;private String address;private Txn txn;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}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 Txn getTxn() {return txn;}public void setTxn(Txn txn) {this.txn = txn;}}

Since we are using XML based configuration for mapping, above model classes are simple POJO classes or Java Beans with getter-setter methods. I am using class name as Txn to avoid confusion because Hibernate API have a class name as Transaction.

由于我们使用基于XML的配置进行映射,因此上述模型类是简单的POJO类或具有getter-setter方法的Java Bean。 我将类名用作Txn以避免混淆,因为Hibernate API的类名为Transaction

Hibernate一对一映射配置 (Hibernate One to One Mapping Configuration)

Let’s create hibernate one to one mapping configuration files for Txn and Customer tables.

让我们为Txn和Customer表创建Hibernate的一对一映射配置文件。

txn.hbm.xml

txn.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""https://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping><class name="com.journaldev.hibernate.model.Txn" table="TRANSACTION" ><id name="id" type="long"><column name="txn_id" /><generator class="identity" /></id><property name="date" type="date"><column name="txn_date" /></property><property name="total" type="double"><column name="txn_total" /></property><one-to-one name="customer" class="com.journaldev.hibernate.model.Customer"cascade="save-update" /></class></hibernate-mapping>

The important point to note above is the hibernate one-to-one element for customer property.

上面要注意的重要一点是客户财产的Hibernateone-to-one元素。

customer.hbm.xml

customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"https://hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping><class name="com.journaldev.hibernate.model.Customer" table="CUSTOMER"><id name="id" type="long"><column name="txn_id" /><generator class="foreign"><param name="property">txn</param></generator></id><one-to-one name="txn" class="com.journaldev.hibernate.model.Txn"constrained="true"></one-to-one><property name="name" type="string"><column name="cust_name"></column></property><property name="email" type="string"><column name="cust_email"></column></property><property name="address" type="string"><column name="cust_address"></column></property></class></hibernate-mapping>

generator class=”foreign” is the important part that is used for hibernate foreign key implementation.

generator class =“ foreign”是用于Hibernate外键实现的重要部分。

Hibernate配置文件 (Hibernate Configuration File)

Here is the hibernate configuration file for XML based hibernate mapping configuration.

这是基于XML的Hibernate映射配置的Hibernate配置文件。

hibernate.cfg.xml

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""https://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.password">pankaj123</property><property name="hibernate.connection.url">jdbc:mysql://localhost/TestDB</property><property name="hibernate.connection.username">pankaj</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.current_session_context_class">thread</property><property name="hibernate.show_sql">true</property><mapping resource="txn.hbm.xml"/><mapping resource="customer.hbm.xml"/></session-factory>
</hibernate-configuration>

Hibernate configuration file is simple, it has database connection properties and hibernate mapping resources.

Hibernate配置文件很简单,它具有数据库连接属性和hibernate映射资源。

Hibernate SessionFactory实用程序 (Hibernate SessionFactory Utility)

Here is the utility class to create hibernate SessionFactory instance.

这是用于创建HibernateSessionFactory实例的实用程序类。

package com.journaldev.hibernate.util;import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;public class HibernateUtil {private static SessionFactory sessionFactory;private static SessionFactory buildSessionFactory() {try {// Create the SessionFactory from hibernate.cfg.xmlConfiguration configuration = new Configuration();configuration.configure("hibernate.cfg.xml");System.out.println("Hibernate Configuration loaded");ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();System.out.println("Hibernate serviceRegistry created");SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);return sessionFactory;}catch (Throwable ex) {System.err.println("Initial SessionFactory creation failed." + ex);ex.printStackTrace();throw new ExceptionInInitializerError(ex);}}public static SessionFactory getSessionFactory() {if(sessionFactory == null) sessionFactory = buildSessionFactory();return sessionFactory;}
}

That’s it, lets write a test program to test the hibernate one to one mapping xml based configuration.

就是这样,让我们​​编写一个测试程序来测试Hibernate的一对一基于xml的映射配置。

Hibernate一对一映射XML配置测试程序 (Hibernate One to One Mapping XML Configuration Test Program)

In the hibernate one to one mapping example test program, first we will create Txn object and save it. Once it’s saved into database, we will use the generated id to retrieve the Txn object and print it.

在Hibernate的一对一映射示例测试程序中,首先我们将创建Txn对象并将其保存。 一旦将其保存到数据库中,我们将使用生成的ID来检索Txn对象并进行打印。

package com.journaldev.hibernate.main;import java.util.Date;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;import com.journaldev.hibernate.model.Customer;
import com.journaldev.hibernate.model.Txn;
import com.journaldev.hibernate.util.HibernateUtil;public class HibernateOneToOneMain {public static void main(String[] args) {Txn txn = buildDemoTransaction();SessionFactory sessionFactory = null;Session session = null;Transaction tx = null;try{//Get SessionsessionFactory = HibernateUtil.getSessionFactory();session = sessionFactory.getCurrentSession();System.out.println("Session created");//start transactiontx = session.beginTransaction();//Save the Model objectsession.save(txn);//Commit transactiontx.commit();System.out.println("Transaction ID="+txn.getId());//Get Saved Trasaction DataprintTransactionData(txn.getId(), sessionFactory);}catch(Exception e){System.out.println("Exception occured. "+e.getMessage());e.printStackTrace();}finally{if(!sessionFactory.isClosed()){System.out.println("Closing SessionFactory");sessionFactory.close();}}}private static void printTransactionData(long id, SessionFactory sessionFactory) {Session session = null;Transaction tx = null;try{//Get SessionsessionFactory = HibernateUtil.getSessionFactory();session = sessionFactory.getCurrentSession();//start transactiontx = session.beginTransaction();//Save the Model objectTxn txn = (Txn) session.get(Txn.class, id);//Commit transactiontx.commit();System.out.println("Transaction Details=\n"+txn);}catch(Exception e){System.out.println("Exception occured. "+e.getMessage());e.printStackTrace();}}private static Txn buildDemoTransaction() {Txn txn = new Txn();txn.setDate(new Date());txn.setTotal(100);Customer cust = new Customer();cust.setAddress("Bangalore, India");cust.setEmail("pankaj@gmail.com");cust.setName("Pankaj Kumar");txn.setCustomer(cust);cust.setTxn(txn);return txn;}}

Now when we run above one to one mapping in hibernate test program, we get following output.

现在,当我们在Hibernate测试程序中运行一对一映射时,将得到以下输出。

Hibernate Configuration loaded
Hibernate serviceRegistry created
Session created
Hibernate: insert into TRANSACTION (txn_date, txn_total) values (?, ?)
Hibernate: insert into CUSTOMER (cust_name, cust_email, cust_address, txn_id) values (?, ?, ?, ?)
Transaction ID=19
Hibernate: select txn0_.txn_id as txn_id1_1_0_, txn0_.txn_date as txn_date2_1_0_, txn0_.txn_total as txn_tota3_1_0_,
customer1_.txn_id as txn_id1_0_1_, customer1_.cust_name as cust_nam2_0_1_, customer1_.cust_email as cust_ema3_0_1_,
customer1_.cust_address as cust_add4_0_1_ from TRANSACTION txn0_ left outer join CUSTOMER customer1_ on
txn0_.txn_id=customer1_.txn_id where txn0_.txn_id=?
Transaction Details=
19, 100.0, Pankaj Kumar, pankaj@gmail.com, Bangalore, India
Closing SessionFactory

As you can see that it’s working fine and we are able to retrieve data from both the tables using transaction id. Check the SQL used by Hibernate internally to get the data, its using joins to get the data from both the tables.

如您所见,它工作正常,我们能够使用事务ID从两个表中检索数据。 在内部检查Hibernate使用SQL来获取数据,使用Hibernate的联接从两个表中获取数据。

Hibernate一对一映射注释 (Hibernate One to One Mapping Annotation)

In the above section, we saw how to use XML based configuration for hibernate one to one mapping, now let’s see how we can use JPA and Hibernate annotation to achieve the same thing.

在上一节中,我们看到了如何将基于XML的配置用于Hibernate一对一映射,现在让我们看看如何使用JPA和Hibernate批注实现相同的目的。

Hibernate配置文件 (Hibernate Configuration File)

hibernate-annotation.cfg.xml

hibernate-annotation.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""https://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.password">pankaj123</property><property name="hibernate.connection.url">jdbc:mysql://localhost/TestDB</property><property name="hibernate.connection.username">pankaj</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.current_session_context_class">thread</property><property name="hibernate.show_sql">true</property><mapping class="com.journaldev.hibernate.model.Txn1"/><mapping class="com.journaldev.hibernate.model.Customer1"/></session-factory>
</hibernate-configuration>

Hibernate configuration is simple, as you can see that I have two model classes that we will use with annotations – Txn1 and Customer1.

Hibernate配置很简单,您可以看到我有两个模型类,我们将它们与批注Txn1使用Txn1Customer1

Hibernate一对一映射注释示例模型类 (Hibernate One to One Mapping Annotation Example Model Classes)

For hibernate one to one mapping annotation configuration, model classes are the most important part. Let’s see how our model classes look.

对于Hibernate的一对一映射注释配置,模型类是最重要的部分。 让我们看看模型类的外观。

package com.journaldev.hibernate.model;import java.util.Date;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;import org.hibernate.annotations.Cascade;@Entity
@Table(name="TRANSACTION")
public class Txn1 {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)@Column(name="txn_id")private long id;@Column(name="txn_date")private Date date;@Column(name="txn_total")private double total;@OneToOne(mappedBy="txn")@Cascade(value=org.hibernate.annotations.CascadeType.SAVE_UPDATE)private Customer1 customer;@Overridepublic String toString(){return id+", "+total+", "+customer.getName()+", "+customer.getEmail()+", "+customer.getAddress();}//Getter-Setter methods, omitted for clarity
}

Notice that most of the annotations are from Java Persistence API because Hibernate provide it’s implementation. However for cascading, we would need to use Hibernate annotation org.hibernate.annotations.Cascade and enum org.hibernate.annotations.CascadeType.

注意,大多数注释来自Java Persistence API,因为Hibernate提供了它的实现。 但是,对于级联,我们需要使用Hibernate批注org.hibernate.annotations.Cascade和枚举org.hibernate.annotations.CascadeType

package com.journaldev.hibernate.model;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;@Entity
@Table(name="CUSTOMER")
public class Customer1 {@Id@Column(name="txn_id", unique=true, nullable=false)@GeneratedValue(generator="gen")@GenericGenerator(name="gen", strategy="foreign", parameters={@Parameter(name="property", value="txn")})private long id;@Column(name="cust_name")private String name;@Column(name="cust_email")private String email;@Column(name="cust_address")private String address;@OneToOne@PrimaryKeyJoinColumnprivate Txn1 txn;//Getter-Setter methods
}

Note that we would need to @GenericGenerator so that id is used from the txn rather than generating it.

注意,我们需要@GenericGenerator以便从txn使用id而不是生成它。

Hibernate SessionFactory Utility类 (Hibernate SessionFactory Utility class)

Creating SessionFactory is independent of the way we provide hibernate mapping. Our utility class for creating SessionFactory looks like below.

创建SessionFactory独立于我们提供Hibernate映射的方式。 我们用于创建SessionFactory的实用程序类如下所示。

package com.journaldev.hibernate.util;import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;public class HibernateAnnotationUtil {private static SessionFactory sessionFactory;private static SessionFactory buildSessionFactory() {try {// Create the SessionFactory from hibernate-annotation.cfg.xmlConfiguration configuration = new Configuration();configuration.configure("hibernate-annotation.cfg.xml");System.out.println("Hibernate Annotation Configuration loaded");ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();System.out.println("Hibernate Annotation serviceRegistry created");SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);return sessionFactory;}catch (Throwable ex) {System.err.println("Initial SessionFactory creation failed." + ex);ex.printStackTrace();throw new ExceptionInInitializerError(ex);}}public static SessionFactory getSessionFactory() {if(sessionFactory == null) sessionFactory = buildSessionFactory();return sessionFactory;}
}

Hibernate一对一映射注释示例测试程序 (Hibernate One to One Mapping Annotation Example Test Program)

Here is a simple test program for our hibernate one to one mapping annotation example.

这是Hibernate的一对一映射注释示例的简单测试程序。

package com.journaldev.hibernate.main;import java.util.Date;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;import com.journaldev.hibernate.model.Customer1;
import com.journaldev.hibernate.model.Txn1;
import com.journaldev.hibernate.util.HibernateAnnotationUtil;public class HibernateOneToOneAnnotationMain {public static void main(String[] args) {Txn1 txn = buildDemoTransaction();SessionFactory sessionFactory = null;Session session = null;Transaction tx = null;try{//Get SessionsessionFactory = HibernateAnnotationUtil.getSessionFactory();session = sessionFactory.getCurrentSession();System.out.println("Session created using annotations configuration");//start transactiontx = session.beginTransaction();//Save the Model objectsession.save(txn);//Commit transactiontx.commit();System.out.println("Annotation Example. Transaction ID="+txn.getId());//Get Saved Trasaction DataprintTransactionData(txn.getId(), sessionFactory);}catch(Exception e){System.out.println("Exception occured. "+e.getMessage());e.printStackTrace();}finally{if(sessionFactory != null && !sessionFactory.isClosed()){System.out.println("Closing SessionFactory");sessionFactory.close();}}}private static void printTransactionData(long id, SessionFactory sessionFactory) {Session session = null;Transaction tx = null;try{//Get SessionsessionFactory = HibernateAnnotationUtil.getSessionFactory();session = sessionFactory.getCurrentSession();//start transactiontx = session.beginTransaction();//Save the Model objectTxn1 txn = (Txn1) session.get(Txn1.class, id);//Commit transactiontx.commit();System.out.println("Annotation Example. Transaction Details=\n"+txn);}catch(Exception e){System.out.println("Exception occured. "+e.getMessage());e.printStackTrace();}}private static Txn1 buildDemoTransaction() {Txn1 txn = new Txn1();txn.setDate(new Date());txn.setTotal(100);Customer1 cust = new Customer1();cust.setAddress("San Jose, USA");cust.setEmail("pankaj@yahoo.com");cust.setName("Pankaj Kr");txn.setCustomer(cust);cust.setTxn(txn);return txn;}}

Here is the output snippet when we execute above program.

这是我们执行上述程序时的输出片段。

Hibernate Annotation Configuration loaded
Hibernate Annotation serviceRegistry created
Session created using annotations configuration
Hibernate: insert into TRANSACTION (txn_date, txn_total) values (?, ?)
Hibernate: insert into CUSTOMER (cust_address, cust_email, cust_name, txn_id) values (?, ?, ?, ?)
Annotation Example. Transaction ID=20
Hibernate: select txn1x0_.txn_id as txn_id1_1_0_, txn1x0_.txn_date as txn_date2_1_0_, txn1x0_.txn_total as txn_tota3_1_0_,
customer1x1_.txn_id as txn_id1_0_1_, customer1x1_.cust_address as cust_add2_0_1_, customer1x1_.cust_email as cust_ema3_0_1_,
customer1x1_.cust_name as cust_nam4_0_1_ from TRANSACTION txn1x0_ left outer join CUSTOMER customer1x1_ on
txn1x0_.txn_id=customer1x1_.txn_id where txn1x0_.txn_id=?
Annotation Example. Transaction Details=
20, 100.0, Pankaj Kr, pankaj@yahoo.com, San Jose, USA
Closing SessionFactory

Notice that the output is similar to hibernate one to one XML based configuration.

请注意,输出类似于Hibernate一对一基于XML的配置。

That’s all for Hibernate One to One mapping example, you can download the final project from below link and play around with it to learn more.

这就是Hibernate一对一映射示例的全部内容,您可以从下面的链接下载最终项目并进行试用以了解更多信息。

Download Hibernate OneToOne Mapping Project下载Hibernate OneToOne映射项目

翻译自: https://www.journaldev.com/2916/hibernate-one-to-one-mapping-example-annotation

hibernate注释映射

hibernate注释映射_Hibernate一对一映射示例注释相关推荐

  1. MyBatis-20MyBatis高级结果映射【一对一映射(4种方式)】

    文章目录 概述 需求 方式一:使用自动映射处理一对一映射 实体类改造 UserMapper接口增加接口方法 UserMapper.xml增加SQL 单元测试 方式二:使用resultMap配置一对一映 ...

  2. 【Mybatis高级映射】一对一映射、一对多映射、多对多映射

    前言 当我们学习heribnate的时候,也就是SSH框架的网上商城的时候,我们就学习过它对应的高级映射,一对一映射,一对多映射,多对多映射.对于SSM的Mybatis来说,肯定也是差不多的.既然开了 ...

  3. ef多条件映射_Hibernate一对一关系映射

    1.需求:新增一个用户时,同时增加身份证信息. 2.数据库设计: 3.说明:以上是基于外键的映射关系,将用户表主键 user_id作为身份证表的外键字段关联起来,作为单独的外键列,保存在数据库中,类似 ...

  4. Hibernate基于主键一对一映射操作实例

    背景: 公民和公民的身份证,是一个一对一的例子,一个公民只有一个身份证. 这个是基于主键的,基于外键的映射在:http://blog.csdn.net/nthack5730/article/detai ...

  5. MyBatis从入门到精通(九):MyBatis高级结果映射之一对一映射

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 本篇博客主要讲解MyBatis中实现查 ...

  6. Hibernate(六)一对一映射关系

    One to One 映射关系(只有当映射自定义对象时,才会用,hibernate支持的java类型都可直接设成属性,了解第一和第二种的xml即可) 基于外键的单向一对一 (xml和annotatio ...

  7. Hibernate关联关系映射-----单向一对一映射配置

    2019独角兽企业重金招聘Python工程师标准>>> 这里举了一夫一妻的例子. 实体: package uni.one2one;public class Husband {priv ...

  8. mybatis foreach 错误_MyBatis高级结果映射之一对一映射

    # 使用别名实现自动映射 假设有这样1个需求:根据用户id查询用户信息的同时获取用户拥有的角色,为了举例,我们假设一个用户只能拥有一个角色(实际情况肯定不是这样的). 一般情况下,不建议直接修改数据库 ...

  9. Hibernate一对一映射示例注释

    Hibernate一对一映射示例注释 今天我们将研究Hibernate中的一对一映射.我们将使用Annotation和XML配置来研究Hibernate One To One Mapping示例. 目 ...

最新文章

  1. 模仿虚基类和抽象方法
  2. 树莓派安装Ubuntu 18 64系统
  3. 报错,但不影响运行ERROR: JDWP Unable to get JNI 1.2 environment, jvm-GetEnv() return code = -2...
  4. Spring JpaRepository示例(内存中)
  5. 基于ZYNQ FPGA实现数据采集与传输系统设计
  6. abcdefg顺序Java打印,全国2012年10月自考JAVA语言程序设计(一)试题及答案
  7. 力扣150. 逆波兰表达式求值(JavaScript)
  8. 继CDH收费之后,这家公司率先推出了免费版大数据套件服务!
  9. 数字孪生堆场智慧安全管控平台
  10. 如何使用word删除签名图片背景
  11. Linux centos安装chromium
  12. SSM框架-Spring(一)
  13. [转载]一位也许是真正的hack说的话
  14. Java编程工具(10-1):idea moudle没有蓝色的小方块
  15. html中hr标签有哪些属性,hr标签的属性有哪些?
  16. 大数据—数据收集系统介绍(Flume,Sqoop)
  17. 人物志 | 美团无人机毛一年:建成空中机器人物流网络是技术人一生的梦想
  18. 编程语言与冯诺伊曼体系结构
  19. 81.(cesium之家)cesium修改灰色背景(默认蓝色)
  20. python修改微信和支付宝步数

热门文章

  1. [转载] Python---函数式编程(map()、filter()和reduce())总结
  2. [转载] Python str title()方法
  3. [转载] numpy数组遍历找到个数最多的元素
  4. [转载] python中异常处理的四个句子_Python学习笔记总结(四)异常处理
  5. 记一次被自己DDOS攻击
  6. python @的作用
  7. 算法复习——割点(洛谷3388)
  8. NPM私有服务器搭建方法——sinopia
  9. 学用MVC4做网站五:5.4删除文章
  10. stl之set集合容器应用基础