HQL比较现有的Objects表

时间:2017-08-02 05:57:12

标签: java sql hibernate spring-mvc hql

我只想解释以下情况。

我有员工的注册

它有一个像BranchAddress这样的字段,我使用 ManyToOne 映射表分支

@Entity
@Table(name = "temp_reg")
public class TemporaryRegistrationDTO {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int ID;

@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;

@ManyToOne(cascade=CascadeType.ALL)
private BranchDTO companyBranch;
public BranchDTO getCompanyBranch() {
    return companyBranch;
}
public void setCompanyBranch(BranchDTO companyBranch) {
    this.companyBranch = companyBranch;
}
public int getID() {
    return ID;
}
public void setID(int iD) {
    ID = iD;
}

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}

}

@Entity
@Table(name = "company_branch")
public class BranchDTO {

@Id
@GeneratedValue ( strategy = GenerationType.AUTO)
private int branchID;
public int getBranchID() {
    return branchID;
}
public void setBranchID(int branchID) {
    this.branchID = branchID;
}
@ManyToOne(cascade=CascadeType.ALL)
private CountriesDTO country;
@ManyToOne(cascade=CascadeType.ALL)
private StatesDTO state;
public CountriesDTO getCountry() {
    return country;
}
public void setCountry(CountriesDTO country) {
    this.country = country;
}
public StatesDTO getState() {
    return state;
}
public void setState(StatesDTO state) {
    this.state = state;
}

}

@Entity
@Table(name = "company_countries")
public class CountriesDTO {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

private String countryCode;
public String getCountryCode() {
    return countryCode;
}
public void setCountryCode(String countryCode) {
    this.countryCode = countryCode;
}

private String countryName;
public String getCountryName() {
    return countryName;
}
public void setCountryName(String countryName) {
    this.countryName = countryName;
}

}

@Entity
@Table(name = "company_states")
public class StatesDTO {

@Id
@GeneratedValue ( strategy = GenerationType.AUTO)
private int state_id;
private String state;
private String stateCode;
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}

public String getStateCode() {
    return stateCode;
}
public void setStateCode(String stateCode) {
    this.stateCode = stateCode;
}
@Override
public String toString() {
    return "StatesDTO [statesID=" + state_id + ", state=" + state + ", stateCode=" + stateCode + "]";
}
public int getState_id() {
    return state_id;
}
public void setState_id(int state_id) {
    this.state_id = state_id;
}   

}

现在,我想要的是,无论何时他们是注册请求,首先我要检查分支地址是否在分支表中可用。如果它已经包含一个条目,那么它将检索分支行并从相同的数据扩展到分支表

现在,为了检查 BranchDTO ,我在 Branch Repository 类中创建了方法。

@Query("from BranchDTO where country = :country and state = :state")
public BranchDTO existsEntry(@org.springframework.data.repository.query.Param("country") CountriesDTO country,@org.springframework.data.repository.query.Param("state") StatesDTO state);

但它反映了我的错误,

object references an unsaved transient instance - save the transient instance before flushing: com.example.demo.pojo.CountriesDTO

谢谢你们

0 个答案:

没有答案