📜  使用注释休眠多对一示例

📅  最后修改于: 2021-01-02 15:46:12             🧑  作者: Mango

使用批注休眠多对一映射

在多对一映射中,各种属性只能称为一个属性。

在此示例中,每个员工只有一个公司地址,而一个地址属于许多员工。在这里,我们将使用注释执行多对一映射。

让我们看一下持久化类

1)一对一映射的持久类

有两个持久性类Employee.java和Address.java。雇员类包含地址类引用,反之亦然。

Employee.java

package com.javatpoint;
import javax.persistence.*;

@Entity
@Table(name="emp107")
public class Employee {  
     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)    
private int employeeId;  
private String name,email;  
@ManyToOne(cascade=CascadeType.ALL)
private Address address;
public int getEmployeeId() {
    return employeeId;
}
public void setEmployeeId(int employeeId) {
    this.employeeId = employeeId;
}
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 Address getAddress() {
    return address;
}
public void setAddress(Address address) {
    this.address = address;
}  
}

地址.java

package com.javatpoint;

import javax.persistence.*;

@Entity
@Table(name="address107")
public class Address {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int addressId;  
    private String addressLine1,city,state,country;  
    private int pincode;  
   @OneToOne(cascade=CascadeType.ALL)
    private Employee employee;
    public int getAddressId() {
        return addressId;
    }
    public void setAddressId(int addressId) {
        this.addressId = addressId;
    }
    public String getAddressLine1() {
        return addressLine1;
    }
    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public int getPincode() {
        return pincode;
    }
    public void setPincode(int pincode) {
        this.pincode = pincode;
    }
    public Employee getEmployee() {
        return employee;
    }
    public void setEmployee(Employee employee) {
        this.employee = employee;
    }  
}

2)在pom.xml文件中添加项目信息和配置。

打开pom.xml文件,然后单击源。现在,在 …. 标记之间添加以下依赖关系。这些依赖关系用于在Maven项目中添加jar文件。

  
    org.hibernate  
    hibernate-core  
    5.3.1.Final  
  
  
    com.oracle  
    ojdbc14  
    10.2.0.4.0  
  

3)配置文件

该文件包含有关数据库和映射文件的信息。

hibernate.cfg.xml

  
  
   
  
  
      
        create  
        org.hibernate.dialect.Oracle9Dialect  
        jdbc:oracle:thin:@localhost:1521:xe  
        system  
        jtp  
        oracle.jdbc.driver.OracleDriver  
      
      
      
  
  

4)用于存储和获取数据的用户类

Store.java

package com.javatpoint;  

import org.hibernate.*;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  
public class Store {  
public static void main(String[] args) {  
    
    StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
    Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();
    
    SessionFactory factory=meta.getSessionFactoryBuilder().build();
    Session session=factory.openSession();
    
    Transaction t=session.beginTransaction();  
      
    Employee e1=new Employee();  
    e1.setName("Ravi Malik");  
    e1.setEmail("ravi@gmail.com");  
    
    Employee e2=new Employee();
    e2.setName("Anuj Verma");
    e2.setEmail("anuj@gmail.com");
      
    Address address1=new Address();  
    address1.setAddressLine1("G-13,Sector 3");  
    address1.setCity("Noida");  
    address1.setState("UP");  
    address1.setCountry("India");  
    address1.setPincode(201301);  
      
    e1.setAddress(address1);  
    e2.setAddress(address1);

    session.persist(e1);  
    session.persist(e2);
    t.commit();  
      
    session.close();  
    System.out.println("success");  
}  
}  

输出值


获取.java

package com.javatpoint;  
import java.util.Iterator;  
import java.util.List;

import javax.persistence.TypedQuery;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
  
public class Fetch {  
public static void main(String[] args) {  
    StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
    Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();
    
    SessionFactory factory=meta.getSessionFactoryBuilder().build();
    Session session=factory.openSession();
      
    TypedQuery query=session.createQuery("from Employee e");  
    List list=query.getResultList(); 
      
    Iterator itr=list.iterator();  
    while(itr.hasNext()){  
     Employee emp=itr.next();  
     System.out.println(emp.getEmployeeId()+" "+emp.getName()+" "+emp.getEmail());  
     Address address=emp.getAddress();  
     System.out.println(address.getAddressLine1()+" "+address.getCity()+" "+  
        address.getState()+" "+address.getCountry()+" "+address.getPincode());  
    }  
  
    session.close();  
    System.out.println("success");  
}  
}  

输出量

输出量