我需要在协会和BlockFloors表之间建立一对多的关系。错误:Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.example.domain.SocietyBlockFloor column: society_id (should be mapped with insert="false" update="false")
SocietyDetails.java
package com.example.domain;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name="society_details")
public class SocietyDetails {
public static final long serialVersionUID = 1L;
@Id
@Column(name = "society_id")
private String societyId;
@Column(name = "society_name")
private String societyName;
@Column (name = "society_address")
private String societyAddress;
@Column (name = "society_country_details_code")
private String societyCountryDetailsCode;
@OneToMany(mappedBy = "societyDetails", cascade = CascadeType.ALL)
private List<SocietyBlockFloor> societyBlockFloor = new ArrayList<SocietyBlockFloor>();
@Override
public String toString() {
return "Society{" +
"societyId='" + societyId + '\'' +
", societyName='" + societyName + '\'' +
", societyAddress='" + societyAddress + '\'' +
", societyCountryDetailsCode='" + societyCountryDetailsCode + '\'' +
", societyBlockFloor='" + societyBlockFloor + '\'' +
'}';
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getSocietyId() {
return societyId;
}
public void setSocietyId(String societyId) {
this.societyId = societyId;
}
public String getSocietyName() {
return societyName;
}
public void setSocietyName(String societyName) {
this.societyName = societyName;
}
public String getSocietyAddress() {
return societyAddress;
}
public void setSocietyAddress(String societyAddress) {
this.societyAddress = societyAddress;
}
public String getSocietyCountryDetailsCode() {
return societyCountryDetailsCode;
}
public void setSocietyCountryDetailsCode(String societyCountryDetailsCode) {
this.societyCountryDetailsCode = societyCountryDetailsCode;
}
public List<SocietyBlockFloor> getSocietyBlockFloor() {
return societyBlockFloor;
}
public void setSocietyBlockFloor(List<SocietyBlockFloor> societyBlockFloor) {
this.societyBlockFloor = societyBlockFloor;
}
}
SocietyBlockFloor.java
package com.example.domain;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name="society_block_floor")
public class SocietyBlockFloor implements Serializable {
public static final long serialVersionUID = 1L;
@Id
@Column(name="society_id")
private String societyBlockFloorId;
@Column(name="society_block")
private String societyBlock;
@Column(name="society_floor")
private String societyFloor;
@ManyToOne
@JoinColumn(name = "society_id", nullable = false)
private SocietyDetails societyDetails;
@Override
public String toString() {
return "HASocietyBlockFloor{" +
"societyBlockFloorId='" + societyBlockFloorId + '\'' +
", societyBlock='" + societyBlock + '\'' +
", societyFloor='" + societyFloor + '\'' +
", societyDetails='" + societyDetails + '\'' +
'}';
}
public String getSocietyBlockFloorId() {
return societyBlockFloorId;
}
public String getSocietyBlock() {
return societyBlock;
}
public void setSocietyBlock(String societyBlock) {
this.societyBlock = societyBlock;
}
public String getSocietyFloor() {
return societyFloor;
}
public void setSocietyFloor(String societyFloor) {
this.societyFloor = societyFloor;
}
public SocietyDetails getSocietyDetails() {
return societyDetails;
}
public void setSocietyDetails(SocietyDetails societyDetails) {
this.societyDetails = societyDetails;
}
}
DAO类
package com.example.dao.impl;
@Repository
public class SocietyDetailsDAOImpl extends BaseDAOImpl implements SocietyDetailsDAO {
@Override
public List<SocietyDetails> getSocietyDetailsByName(String societyName) throws Exception {
try {
String queryString = "from SocietyDetails society JOIN society.societyBlockFloor where society.societyName=:societyName";
Query query = getSessionFactory().getCurrentSession().createQuery(queryString);
query.setParameter("societyName", societyName);
List<SocietyDetails> societyDetails = query.list();
return societyDetails;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
}
我已经尝试过更改SocietyBlockFloor.societyBlockFloorId的名称,但是在ON子句中,它仍然使用(society.societyId = societyBlockFloor.societyId)不存在SocietyBlockFloor.societyId的名称。
最佳答案
您在映射中有重复的列。您两次映射同一数据库列。你有:
@Id
@Column(name="society_id")
private String societyBlockFloorId;
和
@ManyToOne
@JoinColumn(name = "society_id", nullable = false)
private SocietyDetails societyDetails;
这似乎没用
@Id
@Column(name="society_id")
private String societyBlockFloorId;
如果您想获得社会身份,只需问
societyDetails.getId();
而在manyToOne方面
referencedColumnName = "society_id"