问题描述
@Test
@Transactional
public void thatFolderLocationAssociationTableIsWorking(){
Location l1 = new DomesticLocation();
l1.setDeptName(Test name 1);
位置l2 =新的DomesticLocation();
l2.setDeptName(Test name 2);
KMLFolder k1 = new KMLFolder();
k1.setName(Test name 1);
KMLFolder k2 = new KMLFolder();
k1.setName(Test name 2);
列表<位置> locations = new ArrayList< Location>();
locations.add(l1);
locations.add(l2);
k1.setLocations(locations);
kmlFolderServiceImpl.save(k1);
assertEquals(Test name 1,kmlFolderServiceImpl.find(1L).getLocations()。get(0).getDeptName());
assertEquals(Test name 2,kmlFolderServiceImpl.find(1L).getLocations()。get(1).getDeptName());
//下面一行获取NPE
assertEquals(Test name 1,locationServiceImpl.find(1L).getKmlFolderList()。get(0).getName());
}
我正在试图检索laster断言的NPE KMLFolder.getName()
来自 Locations
其他断言正在工作,我得到来自
名称。 KMLFolder
的位置
以下是我的JPA定义:
@ManyToMany(mappedBy =kmlFolderList,cascade = CascadeType.ALL)
private List< Location> locations = new ArrayList< Location>();
@ManyToMany
@JoinTable(name =LOCATION_KMLFOLDER,
joinColumns = {@ JoinColumn(name =KMLFOLDER_ID,referencedColumnName =ID)},
reverseJoinColumns = {@ JoinColumn(name =LOCATION_ID,referencedColumnName =ID)}
)
运行测试时正在创建适当的表。以下是控制台输出:
Hibernate:
create table project.LOCATION_KMLFOLDER(
KMLFOLDER_ID bigint not null,
LOCATION_ID bigint not null
)ENGINE = InnoDB
...
Hibernate:
alter table project.LOCATION_KMLFOLDER
add index FK_lqllrwb2t5cn0cbxxx3ms26ku(LOCATION_ID),
添加约束FK_lqllrwb2t5cn0cbxxx3ms26ku
外键(LOCATION_ID)
引用project.KMLFolder(id)
Hibernate:
alter table .LOCATION_KMLFOLDER
添加索引FK_ckj00nos13yojmcyvtefgk9pl(KMLFOLDER_ID) ,
添加约束FK_ckj00nos13yojmcyvtefgk9pl
外键(KMLFOLDER_ID)
引用project.Locations(id)
控制台没有像我期望的那样显示插入LOCATION_KNLFOLDER表。关于为什么会发生这种情况的任何想法?
您正在初始化关联的反面,Hibernate忽略(或除此之外)初始化关联的所有者端,Hibernate不会忽略。
所有者端是没有 mappedBy 属性。
I'm trying the following unit test:
@Test
@Transactional
public void thatFolderLocationAssociationTableIsWorking() {
Location l1 = new DomesticLocation();
l1.setDeptName("Test name 1");
Location l2 = new DomesticLocation();
l2.setDeptName("Test name 2");
KMLFolder k1 = new KMLFolder();
k1.setName("Test name 1");
KMLFolder k2 = new KMLFolder();
k1.setName("Test name 2");
List<Location> locations = new ArrayList<Location>();
locations.add(l1);
locations.add(l2);
k1.setLocations(locations);
kmlFolderServiceImpl.save(k1);
assertEquals("Test name 1", kmlFolderServiceImpl.find(1L).getLocations().get(0).getDeptName());
assertEquals("Test name 2", kmlFolderServiceImpl.find(1L).getLocations().get(1).getDeptName());
//The following line gets the NPE
assertEquals("Test name 1", locationServiceImpl.find(1L).getKmlFolderList().get(0).getName());
}
I'm getting NPEs on the laster assertions where I'm trying to retrieve KMLFolder.getName()
from the Locations
The other assertions are working, where I get the Location
name from the KMLFolder
.
Here are my JPA definitions:
@ManyToMany(mappedBy="kmlFolderList", cascade=CascadeType.ALL)
private List<Location> locations = new ArrayList<Location>();
@ManyToMany
@JoinTable(name="LOCATION_KMLFOLDER",
joinColumns={@JoinColumn(name="KMLFOLDER_ID", referencedColumnName="ID")},
inverseJoinColumns={@JoinColumn(name="LOCATION_ID", referencedColumnName="ID")}
)
The appropriate table is being created when I run the test. Here's the console output:
Hibernate:
create table project.LOCATION_KMLFOLDER (
KMLFOLDER_ID bigint not null,
LOCATION_ID bigint not null
) ENGINE=InnoDB
...
Hibernate:
alter table project.LOCATION_KMLFOLDER
add index FK_lqllrwb2t5cn0cbxxx3ms26ku (LOCATION_ID),
add constraint FK_lqllrwb2t5cn0cbxxx3ms26ku
foreign key (LOCATION_ID)
references project.KMLFolder (id)
Hibernate:
alter table .LOCATION_KMLFOLDER
add index FK_ckj00nos13yojmcyvtefgk9pl (KMLFOLDER_ID),
add constraint FK_ckj00nos13yojmcyvtefgk9pl
foreign key (KMLFOLDER_ID)
references project.Locations (id)
The console does not show inserts in to the LOCATION_KNLFOLDER table as I expect. Any thoughts on why this may be happening?
You're initializing the inverse side of the association, that Hibernate ignores, instead of (or in addition to) initializing the owner side of the association, that Hibernate doesn't ignore.
The owner side is the side without the mappedBy
attribute.
这篇关于JPA manyToMany不坚持关联表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!