为什么在表中插入记录后休眠更新

时间:2019-03-08 21:03:16

标签: java hibernate

我有两个实体CompanyDetailDriverDetail

公司详细信息

package com.javarnd.pns.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name="company_detail")
public class CompanyDetails {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="company_id")
    private long companyId;

    @Column(name="company_name")
    private String companyName;

    @Column(name="pan_no")
    private String panNo;

    private String website;

    private String email;

    @Column(name="conntact_no")
    private String contactNo;

    @OneToMany(cascade = CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinTable(name = "COMPANY_VEHICLE_DETAIL", joinColumns = @JoinColumn(name = "company_id"), inverseJoinColumns = @JoinColumn(name = "vehicle_id"))
    private List<Vehicle> vehicleList;

    @OneToMany(cascade = CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinTable(name = "COMPANY_DRIVER_DETAIL", joinColumns = @JoinColumn(name = "company_id"), inverseJoinColumns = @JoinColumn(name = "driver_id"))
    private List<DriverDetail> driverList;

    @Column(name="type")
    private String companyType;
    public long getCompanyId() {
        return companyId;
    }

    public void setCompanyId(long companyId) {
        this.companyId = companyId;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getPanNo() {
        return panNo;
    }

    public void setPanNo(String panNo) {
        this.panNo = panNo;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getContactNo() {
        return contactNo;
    }

    public void setContactNo(String contactNo) {
        this.contactNo = contactNo;
    }

    public String getCompanyType() {
        return companyType;
    }

    public void setCompanyType(String companyType) {
        this.companyType = companyType;
    }

    public List<Vehicle> getVehicleList() {
        return vehicleList;
    }

    public void setVehicleList(List<Vehicle> vehicleList) {
        this.vehicleList = vehicleList;
    }

    public List<DriverDetail> getDriverList() {
        return driverList;
    }

    public void setDriverList(List<DriverDetail> driverList) {
        this.driverList = driverList;
    }

}

DriverDetail

package com.javarnd.pns.model;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;

@Entity
public class DriverDetail {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="driver_id")
    private long id;

    private String name;

    @Column(name="contact_no")
    private String contactNo;

    @Column(name="license_no")
    private String licenseNo;

    @ManyToMany(mappedBy="availableDriverList",cascade=CascadeType.ALL)
    private List<Vehicle> asignedVehicle;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="company_id")
    private CompanyDetails companyDetail;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContactNo() {
        return contactNo;
    }

    public String getLicenseNo() {
        return licenseNo;
    }

    public void setLicenseNo(String licenseNo) {
        this.licenseNo = licenseNo;
    }

    public List<Vehicle> getAsignedVehicle() {
        return asignedVehicle;
    }

    public void setAsignedVehicle(List<Vehicle> asignedVehicle) {
        this.asignedVehicle = asignedVehicle;
    }

    public CompanyDetails getCompanyDetail() {
        return companyDetail;
    }

    public void setCompanyDetail(CompanyDetails companyDetail) {
        this.companyDetail = companyDetail;
    }

    public void setContactNo(String contactNo) {
        this.contactNo = contactNo;
    }


}

主要测试班

package com.javarnd.pns.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.javarnd.pns.model.CompanyDetails;
import com.javarnd.pns.model.DriverDetail;
import com.javarnd.pns.service.CompanyDetailService;
import com.javarnd.pns.service.DriverDetailService;

public class TestPns {
    public static void main(String[] args) {
        CompanyDetailService cdService=new CompanyDetailService();
        CompanyDetails cd=new CompanyDetails();
        List<DriverDetail>ddList=new ArrayList<>();
        DriverDetail driver=new DriverDetail();
        DriverDetailService dds=new DriverDetailService();
        Scanner kb=new Scanner(System.in);

        System.out.println("Enter name:");
        String cname=kb.nextLine();
        driver.setName(cname);

        System.out.println("Enter Compnay Id");
        long companyId=kb.nextLong();
        cd.setCompanyId(companyId);

        System.out.println("Enter License:");
        String license=kb.next();
        driver.setLicenseNo(license);

        System.out.println("Enter Contact");    
        String contact=kb.nextLine();
        driver.setContactNo(contact);


        ddList.add(driver);
        cd.setDriverList(ddList);

        driver.setCompanyDetail(cd);
        dds.save(driver);


        System.out.println("saved");
    }
}

现在我在上面的主类中进行了测试,它打印了日志,将其插入DriverDetail表中的值并更新company_detail表,最后将值插入{{ 1}}表

最后是控制台LOG

COMPANY_DRIVER_DETAIL

此后,一切正常,除了Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val= ? where next_val=? Hibernate: /* insert com.javarnd.pns.model.DriverDetail */ insert into DriverDetail (company_id, contact_no, license_no, name, driver_id) values (?, ?, ?, ?, ?) Hibernate: //HERE IT IS UPDATING THE CompanyDetail , MAIKING ALL FIELDS NULL /* update com.javarnd.pns.model.CompanyDetails */ update company_detail set company_name=?, type=?, conntact_no=?, email=?, pan_no=?, website=? where company_id=? Hibernate: /* delete collection com.javarnd.pns.model.CompanyDetails.driverList */ delete from COMPANY_DRIVER_DETAIL where company_id=? Hibernate: /* delete collection com.javarnd.pns.model.CompanyDetails.vehicleList */ delete from COMPANY_VEHICLE_DETAIL where company_id=? Hibernate: /* insert collection row com.javarnd.pns.model.CompanyDetails.driverList */ insert into COMPANY_DRIVER_DETAIL (company_id, driver_id) values (?, ?) 表中特定ID的数据全部为NULL,并且我不想代表ID提取数据,然后将对象传递给{{ 1}},因为它会以某种方式降低性能,所以我需要知道抵抗这种不必要更新的方法。

0 个答案:

没有答案