First, create a maven project, the introduction of dependency, set up a profile
Dependent and set the configuration file can reference previous blog "springdata__jpa"
Second, create entity classes
1.customer class
cn.dzl.jpa.entity Package;
Import the javax.persistence *;.
@Entity
the @Table (name = "cust_customer")
public class the Customer {
// increment primary key set policy
@GeneratedValue (Strategy = GenerationType.IDENTITY)
@Id
Private custId Long;
Private String custName;
Private String custSource;
Private String custIndustry;
Private String custLevel;
Private String custAddress;
Private String custPhone;
@ // PrimaryKeyJoinColumn primary key associated
@OneToOne (cascade = CascadeType.ALL) // set associated cascade effect is, two associated tables, may be separately changed one table
private CustomerExt ext; // this field indicates which table object associated
public CustomerExt getext () {
return EXT;
}
public void setExt(CustomerExt ext) {
this.ext = ext;
}
public long getCustId() {
return custId;
}
public void setCustId(long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custSource='" + custSource + '\'' +
", custIndustry='" + custIndustry + '\'' +
", custLevel='" + custLevel + '\'' +
", custAddress='" + custAddress + '\'' +
", custPhone='" + custPhone + '\'' +
'}';
}
}
2.CustomerExt class
cn.dzl.jpa.entity Package;
Import the javax.persistence *;.
@Entity
the @Table (name = "customer_ext")
public class CustomerExt {
@ // Id on which the comment field, which is the primary key field
@GeneratedValue ( strategy = GenerationType.IDENTITY) // primary key is set to the self-energizing state
Private Long extId;
Private String Memo;
Private String info;
@ // PrimaryKeyJoinColumn annotation indicates that the primary key is the correlation between two
@ OneToOne // between two tables one to one relationship between
private Customer customer; // this field indicate which table object associated
public the getCustomer the Customer () {
return Customer;
}
public void setCustomer (Customer the Customer) {
this.customer = Customer;
}
public Long getExtId () {
return extId;
}
public void setExtId(long extId) {
this.extId = extId;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "CustomerExt{" +
"extId=" + extId +
", memo='" + memo + '\'' +
", info='" + info + '\'' +
'}';
}
}
Third, create dao interface (must carry JpaRepository <entity type, primary key type>)
1.CustomerDao
package cn.dzl.jpa.dao;
import cn.dzl.jpa.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerDao extends JpaRepository<Customer,Long> {
}
2.CustomerExtDao
package cn.dzl.jpa.dao;
import cn.dzl.jpa.entity.CustomerExt;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerExtDao extends JpaRepository<CustomerExt,Long> {
}
四、测试类OneToOne
package cn.dzl;
import cn.dzl.jpa.dao.CustomerDao;
import cn.dzl.jpa.dao.CustomerExtDao;
import cn.dzl.jpa.entity.Customer;
import cn.dzl.jpa.entity.CustomerExt;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Commit;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class OneToOne {
@Autowired
CustomerDao CustomerDao;
@Autowired
CustomerExtDao customerExtDao;
@Test
// change the database, things need to use
the @Transactional
// submit
@Commit
public void addCustomer () {
// create customer objects
the Customer customer = new new the Customer ();
customer.setCustName ( " Ding boss ");
customer.setCustAddress (" Beijing ");
// create customerExt objects
customerExt customerExt = new new customerExt ();
customerExt.setInfo (" good mood today ");
customerExt.setMemo (" play out ");
// Targets association
customer.setExt (customerExt);
customerExt.setCustomer (Customer);
// call the dao layer method saved to the database
customerDao.save(customer);
// customerExtDao.save(customerExt);
}
}