我有一种情况,当我希望它以一对多的方式进行时,延迟加载没有发生。显然,我对此并不陌生。当我调用 s.getSongComposers() 时,为什么无法获取 SongComposers?
这是我的 DAO 课程:
public class SongDAOImpl implements SongDAO {
@PersistenceContext
private EntityManager em;
@Override
@SuppressWarnings(value = { "unchecked" })
public List<Song> getAllSongsOnAlbum(int albumID) {
Query q = em.createQuery("SELECT s FROM Song s WHERE s.album.albumID = :albumid ORDER BY s.songName");
q.setParameter("albumid", albumID);
List<Song> list = (List<Song>) q.getResultList();
for (Song s: list) {
s.getSongComposers();
}
return list;
}
}
这是我的 Song 实体:
@Entity
@Table(name="songs")
public class Song {
@Id
@GeneratedValue
@Column(name="SongID")
private int songID;
@Column(name="SongName")
private String songName;
@Column(name="Length")
private int length;
@ManyToOne
@JoinColumn(name="AlbumID", referencedColumnName="AlbumID")
private Album album;
@OneToMany(mappedBy="song")
private List<SongComposer> songComposers;
.... all getters/setters ......
}
这是我的 SongComposer 实体:
@Entity
@Table(name="song_composer")
public class SongComposer {
@Id
@GeneratedValue
@Column(name="SongComposerID")
private int songComposerID;
@ManyToOne
@JoinColumn(name="SongID", referencedColumnName="SongID")
private Song song;
@ManyToOne
@JoinColumn(name="ComposerID", referencedColumnName="ComposerID")
private Composer composer;
..... getters/setters .......
}