jpa一对一
来源:互联网 发布:mac隐藏文件夹软件 编辑:程序博客网 时间:2024/04/29 00:04
1 一对一单向
package com.demo.beans;
import java.util.Collection;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
@EntityListeners(MyMonitor.class)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String employeeName;
@OneToOne(cascade=CascadeType.ALL) -------指定了级联的设置,类型是all
@JoinColumn(name="AddressId")
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Collection getEmployeePeriod() {
return employeePeriod;
}
public void setEmployeePeriod(Collection employeePeriod) {
this.employeePeriod = employeePeriod;
}
public Set<String> getNickName() {
return nickName;
}
public void setNickName(Set<String> nickName) {
this.nickName = nickName;
}
public String getEmployeeName() {
return employeeName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
}
package com.demo.beans;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name="address")
public class Address {
@Id
private int addressId;
private String addressCountry;
private String addressCity;
public int getAddressId() {
return addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public String getAddressCountry() {
return addressCountry;
}
public void setAddressCountry(String addressCountry) {
this.addressCountry = addressCountry;
}
public String getAddressCity() {
return addressCity;
}
public void setAddressCity(String addressCity) {
this.addressCity = addressCity;
}
}
在测试类中的例子:
public void testontoone() {
Address address = new Address();
address.setAddressCity("beijing");
address.setAddressCountry("haidian");
address.setAddressId(1);
Employee employee = new Employee();
employee.setEmployeeName("John Smith");
employee.setAddress(address);
userRepository.addEmployee(employee);
}
运行后,数据库表如下:
mysql> select * from address; -------因为是级联的,所以自动保存了address的记录
+-----------+-------------+----------------+
| addressId | addressCity | addressCountry |
+-----------+-------------+----------------+
| 1 | beijing | haidian |
+-----------+-------------+----------------+
mysql> select * from employee;
+----+--------------+------------+------------+-----------+
| id | employeeName | end | begin | AddressId |
+----+--------------+------------+------------+-----------+
| 1 | John Smith | NULL | NULL | 1 |
+----+--------------+------------+------------+-----------+
5 rows in set (0.00 sec)
select constraint_name,table_schema,table_name,constraint_type from information_schema.table_constraints where table_schema='test';
FK_lqcfs6nta8r0812ay3qshreo5 | test | employee | FOREIGN KEY
看到关联列是addressid
一对一双向
只需要修改address类即可
package com.demo.beans;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity(name="address")
public class Address {
@Id
private int addressId;
private String addressCountry;
private String addressCity;
@OneToOne(cascade=CascadeType.ALL,mappedBy="address")
private Employee employee;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public int getAddressId() {
return addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public String getAddressCountry() {
return addressCountry;
}
public void setAddressCountry(String addressCountry) {
this.addressCountry = addressCountry;
}
public String getAddressCity() {
return addressCity;
}
public void setAddressCity(String addressCity) {
this.addressCity = addressCity;
}
}
运行后的数据库表中的信息看上去是一样的,不知道单向和双向有什么区别??只是标示了实体的关系?下面是2个注解的说明
Anatomy of @OneToOne Annotation
- Target: fields (Including property get methods)
- Uses: @OneToOne
- Arguments: The @OneToOne annotation takes multiple arguments when it used and these are:
- targetEntity (Optional): It is used to determine the entity class that is the target of the association.
- cascade (Optional): It is used to determine the operations that must be cascaded to the target of the association.
- fetch (Optional): It is used to determine whether the association should be Lazily loaded or must be eagerly fetched. The EAGER strategy is a requirement on the persistence provider runtime that the associated entity must be eagerly fetched. The LAZILY strategy is just a hint to the persistence provider runtime.
- optional (Optional): It is used to determine whether the association is optional. If set to false then a non-null must be always exist.
- mappedBy (Optional): The field that owns the relationship. The mappedBy element is only specified on the inverse (non-owning) side of the association. This argument is very important one and it is will be clarified when we are coming to discuss the Bidirectional and Unidirectional.
Anatomy of @JoinColumn Annotation
- Target: fields (Including property get methods)
- Uses: @JoinColumn
- Arguments:The @OneToOne annotation takes multiple arguments when it used and these are:
- name (Optional): The name of the foreign key column. The table in which it is found depends upon the context.
- referencedColumnName (Optional): The name of the column referenced by this foreign key column.
- unique (Optional): Whether the property is a unique key.
- nullable (Optional): Whether the foreign key is nullable.
- insertable (Optional): Whether the column is included in the SQL INSERT statements generated by the persistence provider.
- updatable (Optional): Whether the column is included in the SQL UPDATE statements generated by the persistence provider.
- columnDefinition (Optional): The SQL fragment that is used when generated the DDL for the column.
- table (Optional): The name of the table that contains the column.
- JPA 一对一
- jpa一对一
- JPA 一对一双向关联
- jpa的一对一
- JPA一对一映射案例
- JPA--双向一对一
- jpa一对一映射案例
- JPA:双向一对一
- JPA之JPA中的双向一对一关联
- hibernate JPA 一对一 onetoone映射
- JPA一对一双向关联实例
- JPA关联映射 - 一对一映射
- jpa双向一对一关联关系
- JPA一对一关系实例配置
- JPA 中 one-toone 一对一 映射
- JPA中的一对多和一对一映射
- JPA中的一对多和一对一映射
- Hibernate JPA学习(二):一对一关系
- 云计算初体验
- 【spring框架】bean的生命周期
- x+=y与x=x+y的区别
- 使用Windbg解析dump文件
- 百度地图开发:如何计算两点之间距离
- jpa一对一
- codeforces 413D 2048 (dp)
- C++ 顺序容器的定义
- UVALive 2775 (LA 2775) Hidden Password 后缀数组
- 设计模式之简单工厂模式
- Ext JS 4官方指南(二)—— 基本概念之Ext JS类体系
- 【百度地图API】如何根据摩卡托坐标进行POI查询,和计算两点距离
- Oracle Database体系结构概述
- 如何通过抓包工具fiddler获取java程序的http请求