我正在尝试从关联N:N获取数据,但收到空白字段。
有我的Hibernate映射类:
@Entity
@Table(name = "companies")
public class Company extends NamedEntity {
//some_code
@ManyToMany(
targetEntity = com.hqcargo.shippingstock.domain.CompanyProfile.class,
cascade = {CascadeType.ALL, CascadeType.ALL },
fetch = FetchType.EAGER
)
@JoinTable(
name = "company_company_profiles",
joinColumns = {@JoinColumn(
name = "company_id",
referencedColumnName = "id") },
inverseJoinColumns = {@JoinColumn(
name = "company_profile_id",
referencedColumnName = "id") }
)
private List<CompanyProfile> profiles;
// getters and setters + other code
其他班:
@Entity
@Table(name = "company_profiles")
public class CompanyProfile extends NamedEntity {
//some_code
@ManyToMany(
targetEntity = com.hqcargo.shippingstock.domain.Company.class,
cascade = {CascadeType.ALL, CascadeType.ALL },
mappedBy = "profiles",
fetch = FetchType.EAGER
)
@JoinTable(
name = "company_company_profiles",
joinColumns = {@JoinColumn(
name = "company_profile_id",
referencedColumnName = "id") },
inverseJoinColumns = {@JoinColumn(
name = "company_id",
referencedColumnName = "id") }
)
private List<Company> companies;
// getters and setters + other code
此类适用于保存,但不适用于加载数据。
下面我添加DAO类:
public class CompanyJpaDAO extends JpaDaoSupport
implements CompanyDAO {
@Transactional(readOnly = true)
public Company readById(Long id) {
Company tempCompany = getJpaTemplate().find(Company.class, id);
return tempCompany;
}
}
@Transactional
public void update(Company existingInstance) {
if (existingInstance.isNew()) {
throw new InvalidDataAccessApiUsageException(
"Can't update record. Given instance don't have assigned ID");
}
save(existingInstance);
}
当我更新一切正常(由Hibernate生成sql)时:
251252 [13627369@qtp-8132032-9] DEBUG org.hibernate.SQL -
update
companies
set
name=?,
where
id=?
Hibernate:
update
companies
set
name=?,
where
id=?
251295 [13627369@qtp-8132032-9] DEBUG org.hibernate.SQL -
insert
into
company_company_profiles
(company_id, company_profile_id)
values
(?, ?)
Hibernate:
insert
into
company_company_profiles
(company_id, company_profile_id)
values
(?, ?)
但是当我加载数据时,我没有从company_company_profiles(Hibernate生成的sql)中获取信息
36088 [13507994@qtp-8132032-13] DEBUG org.hibernate.SQL -
select
companies0_.company_profile_id as company2_8_,
companies0_.company_id as company1_8_,
company1_.id as id3_0_,
company1_.name as name3_0_,
from
company_company_profiles companies0_
left outer join
companies company1_
on companies0_.company_id=company1_.id
where
companies0_.company_profile_id=?
Hibernate:
select
companies0_.company_profile_id as company2_8_,
companies0_.company_id as company1_8_,
company1_.id as id3_0_,
company1_.name as name3_0_,
from
company_company_profiles companies0_
left outer join
companies company1_
on companies0_.company_id=company1_.id
where
companies0_.company_profile_id=?
您在我的代码中看到任何错误吗?
最佳答案
我不确定这是问题的根源,但您不应在多对多关联的两侧指定JoinTable
。根据JPA规范,JoinTable
注释位于关联的所属方(在您的情况下为Company
):
9.1.25 JoinTable注释
JoinTable
批注用于
关联的映射。一种
在JoinTable
注释上指定
多对多的拥有方
关联或单向
一对多关联。
因此,请在CompanyProfile
实体中将其删除:
@Entity
@Table(name = "company_profiles")
public class CompanyProfile extends NamedEntity {
//some_code
@ManyToMany(
targetEntity = com.hqcargo.shippingstock.domain.Company.class,
cascade = {CascadeType.ALL, CascadeType.ALL },
mappedBy = "profiles",
fetch = FetchType.EAGER
)
private List<Company> companies;
// getters and setters + other code
}
然后再试一次。
参考文献
JPA 1.0规范
第2.1.8.4节“双向多对多关系”
第9.1.25节“ JoinTable注释”
第9.1.26节“多对多注释”