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:
  1. targetEntity (Optional): It is used to determine the entity class that is the target of the association.
  2. cascade (Optional): It is used to determine the operations that must be cascaded to the target of the association.
  3. 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.
  4. optional (Optional): It is used to determine whether the association is optional. If set to false then a non-null must be always exist.
  5. 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:
  1. name (Optional): The name of the foreign key column. The table in which it is found depends upon the context.
  2. referencedColumnName (Optional): The name of the column referenced by this foreign key column.
  3. unique (Optional): Whether the property is a unique key.
  4. nullable (Optional): Whether the foreign key is nullable.
  5. insertable (Optional): Whether the column is included in the SQL INSERT statements generated by the persistence provider.
  6. updatable (Optional): Whether the column is included in the SQL UPDATE statements generated by the persistence provider.
  7. columnDefinition (Optional): The SQL fragment that is used when generated the DDL for the column.
  8. table (Optional): The name of the table that contains the column.
- See more at: http://www.javabeat.net/eclipselink-jpa-annotations-onetoone/#sthash.IXjgqnLf.dpuf


0 0
原创粉丝点击