0
我想下面的單元測試:JPA多對多在ASSOCATION表中沒有堅持
@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());
}
我越來越哪裏我試圖找回從Locations
其他斷言是KMLFolder.getName()
在拉斯特斷言的NPE工作,我從KMLFolder
得到Location
名稱。
這裏是我的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")},
inverseJoinColumns={@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),
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)
控制檯不會顯示插入到LOCATION_KNLFOLDER表,如我所料。有關爲什麼會發生這種情況的任何想法?