我有两个具有OneToMany关系的对象,外键是userName
。当我检查数据库时,我可以在那里看到外键。但我不知道为什么我什么也得不到。
JSP
//ERROR HERE, I CAN'T GET DATA OF THE FOREIGN KEY 'USERNAME'
<div class="row">
<label class="col-sm-2">Owner</label>
<div class="col-sm-10">${book.myUserAccount.userName}</div>
</div>
<div class="row">
<label class="col-sm-2">Book's Name</label>
<div class="col-sm-10">${book.tensach}</div>
</div>
<div class="row">
<label class="col-sm-2">Author</label>
<div class="col-sm-10">${book.tacgia}</div>
</div>
<div class="row">
<label class="col-sm-2">Review</label>
<div class="col-sm-10">${book.nhanxet}</div>
</div>
模型MyUserAccount
public class MyUserAccount implements Serializable {
private static final long serialVersionUID = 1L;
public static final String ROLE_USER = "ROLE_USER";
private String id;
private String email;
private String userName;
private String firstName;
private String lastName;
private String password;
private String role;
private String enabled;
private List<Book> book = new ArrayList<Book>(0);
private List<BookTrade> bookTrade = new ArrayList<BookTrade>(0);
public MyUserAccount() {
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "myUserAccount")
public List<Book> getBook() {
return book;
}
public void setBook(List<Book> book) {
this.book = book;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "myUserAccount")
public List<BookTrade> getBookTrade() {
return bookTrade;
}
public void setBookTrade(List<BookTrade> bookTrade) {
this.bookTrade = bookTrade;
}
public MyUserAccount(String id, String email, String userName, String firstName, //
String lastName, String password, String role, String enabled) {
this.id = id;
this.email = email;
this.userName = userName;
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
this.role = role;
this.enabled = enabled;
}
@Column(name = "ID", unique = true, nullable = false)
public String getId() {
return id;
}
模特书
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private Integer book_ID;
private String tensach;
private String tacgia;
private String nhanxet;
private String tinhtrang;
private String theloai;
private byte[] image;
private String image_name;
private String data;
private MyUserAccount myUserAccount;
public Book() {
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "USER_NAME", nullable = false)
public MyUserAccount getMyUserAccount() {
return this.myUserAccount;
}
public void setMyUserAccount(MyUserAccount myUserAccount) {
this.myUserAccount = myUserAccount;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "book_id", unique = true, nullable = false)
public Integer getBook_ID() {
return book_ID;
}
我如何映射两个对象:
控制器
public String saveBook(@ModelAttribute("bookForm") @Validated Book book, BindingResult result, Model model,
@RequestParam CommonsMultipartFile[] image, @RequestParam String userName, final RedirectAttributes redirectAttributes)
throws IOException, UnsupportedEncodingException {
MyUserAccount myUserAccount = myUserAccountDAO.findByUserName(userName);
book.setMyUserAccount(myUserAccount);
redirectAttributes.addFlashAttribute("css", "success");
if (book.getBook_ID() == null) {
System.out.println(book.getBook_ID());
redirectAttributes.addFlashAttribute("msg", "book added successfully!");
} else {
redirectAttributes.addFlashAttribute("msg", "book updated successfully!");
}
for (CommonsMultipartFile aFile : image) {
System.out.println("Saving file: " + aFile.getOriginalFilename());
book.setImage_name(aFile.getOriginalFilename());
book.setImage(aFile.getBytes());
System.out.println("Damn that Shit!");
}
bookService.saveOrUpdate(book);
// POST/REDIRECT/GET
return "redirect:/motsach/" + book.getBook_ID();
}
DAO
public void save(Book book) {
// TODO Auto-generated method stub
KeyHolder keyHolder = new GeneratedKeyHolder();
String sql = "INSERT INTO Books(TENSACH, TACGIA, NHANXET, TINHTRANG, THELOAI, IMAGE, IMAGE_NAME, USER_NAME) "
+ "VALUES ( :tensach, :tacgia, :nhanxet, :tinhtrang, :theloai, :image, :image_name, :user_name)";
namedParameterJdbcTemplate.update(sql, getSqlParameterByModel(book), keyHolder);
book.setBook_ID(keyHolder.getKey().intValue());
}
private SqlParameterSource getSqlParameterByModel(Book book) {
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("book_id", book.getBook_ID());
paramSource.addValue("tensach", book.getTensach());
paramSource.addValue("tacgia", book.getTacgia());
paramSource.addValue("nhanxet", book.getNhanxet());
paramSource.addValue("tinhtrang", book.getTinhtrang());
paramSource.addValue("image", book.getImage());
paramSource.addValue("image_name", book.getImage_name());
paramSource.addValue("theloai", book.getTheloai());
paramSource.addValue("user_name", book.getMyUserAccount().getUserName());
return paramSource;
}