📜  Hibernate – 一对多映射

📅  最后修改于: 2022-05-13 01:55:33.430000             🧑  作者: Mango

Hibernate – 一对多映射

Spring Boot 建立在 Spring 之上,包含了 Spring 的所有特性。 Spring 还提供了 JPA 和 hibernate 来提高 Spring 应用程序和数据库之间的数据操作效率。简单来说,我们可以说 JPA(Java持久性 API)就像一个接口,而休眠是接口方法的实现,就像在休眠的帮助下已经定义了插入的方式一样。在本文中,我们将讨论 Hibernate 中的一对多映射。让我们借助一个真实的例子来理解一对多映射。自行车制造商可以制造多种型号的自行车,但同一型号的自行车不能由多个制造商制造。

语法

Spring Initializer 是一个基于 Web 的工具,我们可以使用它轻松生成 Spring Boot 项目的结构。它还为元数据模型中表达的项目提供各种不同的功能。该模型允许我们配置 JVM 支持的依赖项列表。在这里,我们将使用 spring 初始化程序创建应用程序的结构。

例子

第 1 步:转到此链接。根据要求填写详细信息。对于此应用程序:

Project: Maven
Language: Java
Spring Boot: 2.5.7
Packaging: JAR
Java: 11
Dependencies: Spring Web,Spring Data JPA, MySql Driver

单击生成将下载启动项目。

第 2 步:解压缩 zip 文件。现在打开一个合适的 IDE,然后转到 File > New > Project from existing sources > Spring-boot-app 并选择 pom.xml。点击提示导入更改,等待项目同步,如下图所示:

项目结构:

一对多映射

第 3 步:在 application.properties 文件中添加必要的属性。 (映射是数据库名称)

spring.datasource.username=root
spring.datasource.password=Aayush
spring.datasource.url=jdbc:mysql://localhost:3306/mapping
spring.jpa.hibernate.ddl-auto=update

第 4 步:转到 src->main-> Java->com->example->Mapping 并在模型文件夹中创建两个文件,即 Manufactures。 Java和模型。Java

项目结构:

制成品。Java

Java
package com.example.Mapping.Models;
 
import javax.persistence.*;
import java.util.List;
 
@Entity
public class Manufactures {
   
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    // One manufactures can produces
    // multiple models of the bike
    private  String manufactures_name;
 
    @OneToMany(mappedBy ="ob")
 
    private Listmodels;
 
    public Manufactures(int id, String manufactures_name) {
        this.id = id;
        this.manufactures_name = manufactures_name;
    }
   
    Manufactures(){
 
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getManufactures_name() {
        return manufactures_name;
    }
 
    public void setManufactures_name(String manufactures_name) {
        this.manufactures_name = manufactures_name;
    }
}


Java
package com.example.Mapping.Models;
 
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
 
@Entity
public class Model {
   
    @Id
    private int model_id;
 
    private String name;
 
    @ManyToOne
    @JoinColumn(name = "manufacture_id")
    private Manufactures ob;
 
    public Model(int model_id, String name, Manufactures ob) {
        this.model_id = model_id;
        this.name = name;
        this.ob = ob;
    }
   
    Model(){
 
    }
 
    public int getModel_id() {
        return model_id;
    }
 
    public void setModel_id(int model_id) {
        this.model_id = model_id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Manufactures getOb() {
        return ob;
    }
 
    public void setOb(Manufactures ob) {
        this.ob = ob;
    }
}


Java
package com.example.Mapping.Reposteries;
 
import com.example.Mapping.Models.Manufactures;
import org.springframework.data.jpa.repository.JpaRepository;
 
public interface ManufacturesRepo extends JpaRepository {
   
}


Java
package com.example.Mapping.Reposteries;
 
import com.example.Mapping.Models.Model;
import org.springframework.data.jpa.repository.JpaRepository;
 
public interface ModelRepo extends JpaRepository {
}


Java
package com.example.Mapping;
 
import com.example.Mapping.Models.Manufactures;
import com.example.Mapping.Models.Model;
import com.example.Mapping.Reposteries.ManufacturesRepo;
import com.example.Mapping.Reposteries.ModelRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class MappingApplication implements CommandLineRunner {
 
    @Autowired
    ManufacturesRepo manufacturesRepo;
   
    @Autowired
    ModelRepo modelRepo;
 
    public static void main(String[] args) {
        SpringApplication.run(MappingApplication.class, args);
    }
     
    @Override
    public void run(String... args) throws Exception {
     
    Manufactures data=new Manufactures(1,"Honda");
    
    // Inserting the record in the Manufactures table.
    manufacturesRepo.save(data);
    
        // Now try to mapped above record with multiple models
        Model model1=new Model(1,"AYZ",data);
        Model model2=new Model(2,"ZET",data);
        modelRepo.save(model1);
        modelRepo.save(model2);
 
    }
}


型号(按表映射)

Java

package com.example.Mapping.Models;
 
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
 
@Entity
public class Model {
   
    @Id
    private int model_id;
 
    private String name;
 
    @ManyToOne
    @JoinColumn(name = "manufacture_id")
    private Manufactures ob;
 
    public Model(int model_id, String name, Manufactures ob) {
        this.model_id = model_id;
        this.name = name;
        this.ob = ob;
    }
   
    Model(){
 
    }
 
    public int getModel_id() {
        return model_id;
    }
 
    public void setModel_id(int model_id) {
        this.model_id = model_id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Manufactures getOb() {
        return ob;
    }
 
    public void setOb(Manufactures ob) {
        this.ob = ob;
    }
}

第 5 步:在项目结构中添加两个类的 JPA 存储库:

制造回购

Java

package com.example.Mapping.Reposteries;
 
import com.example.Mapping.Models.Manufactures;
import org.springframework.data.jpa.repository.JpaRepository;
 
public interface ManufacturesRepo extends JpaRepository {
   
}

模型回购:

Java

package com.example.Mapping.Reposteries;
 
import com.example.Mapping.Models.Model;
import org.springframework.data.jpa.repository.JpaRepository;
 
public interface ModelRepo extends JpaRepository {
}

映射应用:

Java

package com.example.Mapping;
 
import com.example.Mapping.Models.Manufactures;
import com.example.Mapping.Models.Model;
import com.example.Mapping.Reposteries.ManufacturesRepo;
import com.example.Mapping.Reposteries.ModelRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class MappingApplication implements CommandLineRunner {
 
    @Autowired
    ManufacturesRepo manufacturesRepo;
   
    @Autowired
    ModelRepo modelRepo;
 
    public static void main(String[] args) {
        SpringApplication.run(MappingApplication.class, args);
    }
     
    @Override
    public void run(String... args) throws Exception {
     
    Manufactures data=new Manufactures(1,"Honda");
    
    // Inserting the record in the Manufactures table.
    manufacturesRepo.save(data);
    
        // Now try to mapped above record with multiple models
        Model model1=new Model(1,"AYZ",data);
        Model model2=new Model(2,"ZET",data);
        modelRepo.save(model1);
        modelRepo.save(model2);
 
    }
}

运行主应用程序:

制造表

模型表