JPA, Spring, Hibernate problem with loading entities ManyToMany Assotiation
I'm trying to get data from assotiation N:N, but I receive blank field.
There are my Hibernate mapping class:@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
other class:
@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") }
开发者_JAVA百科 )
private List<Company> companies;
// getters and setters + other code
This classes works fine with saving but not with loading data.
Below I add DAO class: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);
}
when I update every is ok (generated sql by Hibernate):
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
(?, ?)
but when I load data i don't get info from company_company_profiles (sql generated by Hibernate)
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=?
Do you see any mistakes in my code?
I'm not sure this is the cause of the problem but you are not supposed to specify a JoinTable
on both sides of your many-to-many association. As per the JPA specification, the JoinTable
annotation goes on the the owning side of the association (Company
in your case):
9.1.25 JoinTable Annotation
The
JoinTable
annotation is used in the mapping of associations. AJoinTable
annotation is specified on the owning side of a many-to-many association, or in a unidirectional one-to-many association.
So remove it in the CompanyProfile
entity:
@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
}
And try again.
References
- JPA 1.0 specification
- Section 2.1.8.4 "Bidirectional ManyToMany Relationships"
- Section 9.1.25 "JoinTable Annotation"
- Section 9.1.26 "ManyToMany Annotation"
精彩评论