gpt4 book ai didi

java - hibernate中的LEFT JOIN表映射

转载 作者:行者123 更新时间:2023-11-29 16:01:18 25 4
gpt4 key购买 nike

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:[com.websystique.springmvc.model.AssetMakeMaster#0]

我有两个实体如下:1. Assets 大师2.AssetMakeMaster

以上实体与表进行了映射。我想对上述两个实体使用左连接并从 AssetMaster 获取所有数据。内部查询对于上述实体工作正常,但我也想获得不匹配的值。我已经尝试过但尚未得到解决方案。请任何人都可以解决我的问题。

我在 mysql 中尝试了以下查询,它给了我正确的结果,但我想从 hibernate 实体获取结果。请给我以下查询的解决方案:

SELECT * FROM fscassets.asset_master a left join fscassets.asset_make_master b on a.make=b.id;

/*  * To change this license header, choose License Headers in Project
Properties. * To change this template file, choose Tools | Templates
* and open the template in the editor. */ package > >
/** * * @author Amol */
@Entity @Table(name = "asset_master")
public class AssetMaster implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "make", nullable = false)
private int make;

@Column(name = "serialno", nullable = false)
private String serialno;

@Column(name = "specs", nullable = false)
private String specs;

@Column(name = "model", nullable = false)
private String model;

@Column(name = "quantity", nullable = false)
private int quantity;

@Column(name = "purchasedate", nullable = false)
private String purchasedate;

@Column(name = "created_on", nullable = false)
private String createdOnDate;

@Column(name = "remark", nullable = false)
private String remark;

@Column(name = "ipaddress", nullable = false)
private String ipAddress;


@ManyToOne(cascade = CascadeType.ALL)
@Fetch(FetchMode.SELECT)
@JoinColumn(name = "make", updatable = false, insertable = false)
private AssetMakeMaster assetMakeMasterdata;

public AssetMakeMaster getAssetMakeMasterdata() {
return assetMakeMasterdata;
}

public void setAssetMakeMasterdata(AssetMakeMaster assetMakeMasterdata) {
this.assetMakeMasterdata = assetMakeMasterdata;
}



public Integer getId() {
return id;
}

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


public String getCreatedOnDate() {
return createdOnDate;
}

public void setCreatedOnDate(String createdOnDate) {
this.createdOnDate = createdOnDate;
}

public String getIpAddress() {
return ipAddress;
}

public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}


public int getMake() {
return make;
}

public void setMake(int make) {
this.make = make;
}

public String getSerialno() {
return serialno;
}

public void setSerialno(String serialno) {
this.serialno = serialno;
}

public String getSpecs() {
return specs;
}

public void setSpecs(String specs) {
this.specs = specs;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public String getPurchasedate() {
return purchasedate;
}

public void setPurchasedate(String purchasedate) {
this.purchasedate = purchasedate;
}


public String getRemark() {
return remark;
}

public void setRemark(String remark) {
this.remark = remark;
}

}

/*  * To change this license header, choose License Headers in Project
Properties. * To change this template file, choose Tools | Templates
* and open the template in the editor. */ package > >
/** * * @author Amol */

@Entity @Table(name = "asset_make_master") public class AssetMakeMaster implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@NotEmpty
@Column(name = "asset_make_name", unique = true, nullable = false)
private String assetmakename;

@Column(name = "assetgroup", nullable = false)
private int groupid;

@Column(name = "created_on", nullable = false)
private String createdOnDate;

@Column(name = "ipaddress", nullable = false)
private String ipAddress;


@ManyToMany(cascade = CascadeType.ALL, mappedBy = "assetMakeMasterdata")
private List<AssetMaster> assetMakeMasters = new ArrayList<AssetMaster>();

public List<AssetMaster> getAssetMakeMasters() {
return assetMakeMasters;
}

public void setAssetMakeMasters(List<AssetMaster> assetMakeMasters) {
this.assetMakeMasters = assetMakeMasters;
}
public Integer getId() {
return id;
}

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

public String getAssetmakename() {
return assetmakename;
}

public void setAssetmakename(String assetmakename) {
this.assetmakename = assetmakename;
}

public int getGroupid() {
return groupid;
}

public void setGroupid(int groupid) {
this.groupid = groupid;
}

public String getCreatedOnDate() {
return createdOnDate;
}

public void setCreatedOnDate(String createdOnDate) {
this.createdOnDate = createdOnDate;
}

public String getIpAddress() {
return ipAddress;
}

public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}

}

最佳答案

您不能在 HQL 查询中使用 (*)。如果您还想要不匹配的值,请单独选择该列,您将获得值。

关于java - hibernate中的LEFT JOIN表映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56182994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com