2015-12-04 169 views
1

這是我第一個使用Spring/Spring Boot的項目。這是一個使用User和Bookmark實體的簡單書籤服務。對於每個控制器,創建/發佈DTO時會創建並驗證。如果DTO有效,我實例化一個新的用戶或書籤。Spring Boot,JPA/Hibernate DTO,ManyToOne

我遇到的問題是,當我創建一個新書籤時,書籤表中的user_id列爲空。我將發佈以兩個域對象,DTO和控制器開始的代碼。

用戶

@Entity 
public class User { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long id; 

@Column(unique = true, nullable = false) 
private String username; 
private String password; 

@Column(unique = true, nullable = false) 
private String email; 

@Column(name = "role", nullable = false) 
@Enumerated(EnumType.STRING) 
private Role role; 

@OneToMany(mappedBy = "user") 
private List<Bookmark> bookmarks; 

public User() { } 

public Long getId() { 
    return id; 
} 

//getters and setters and toString. 
} 

書籤

@Entity 
public class Bookmark { 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
private Long id; 

private String url; 

@Column(length = 2048) 
private String description; 

@ManyToOne() 
private User user; 

public Bookmark() { } 

//getters and setters and toString. 
} 

用戶DTO

public class UserCreateForm { 

@NotEmpty 
private String username = ""; 

@NotEmpty 
private String password = ""; 

@NotEmpty 
private String passwordConfirmation = ""; 

@NotEmpty 
@Email 
private String email = ""; 

@NotNull 
private Role role = Role.USER; 

//getters and setters and toString. 
} 

書籤DTO

public class BookmarkCreateForm { 

@NotEmpty 
@URL 
private String url=""; 

private String description=""; 

private Long userId; 

public BookmarkCreateForm() { } 

//getters and setters and toString. 
} 

真的只有書籤控制器在這裏很重要,所以我會跳過UserController,但可以根據需要添加它。

BookmarkController

@RestController 
public class BookmarkController { 

private final UserService userService; 

private final BookmarkSerivce bookmarkSerivce; 

@Autowired 
public BookmarkController(UserService userService, BookmarkSerivce bookmarkSerivce) { 
    this.userService = userService; 
    this.bookmarkSerivce = bookmarkSerivce; 
} 

@RequestMapping("{userId}/add") 
public Bookmark addBookmark(@PathVariable("userId") Long userId, @RequestBody BookmarkCreateForm form) { 

    User user = userService.getUserById(userId) 
      .orElseThrow(() -> new NoSuchElementException(String.format("User=%s not found", userId))); 

    System.out.println(form.getUserId()); 
    Bookmark bookmark = bookmarkSerivce.create(form); 

    return bookmark; 
} 

}

而且BookmarkServiceImpl

@Service 
public class BookmarkServiceImpl implements BookmarkSerivce { 

private final BookmarkRepository bookmarkRepository; 

@Autowired 
public BookmarkServiceImpl(BookmarkRepository bookmarkRepository) { 
    this.bookmarkRepository = bookmarkRepository; 
} 

@Override 
public Bookmark create(BookmarkCreateForm form) { 
    Bookmark bookmark = new Bookmark(); 
    bookmark.setUrl(form.getUrl()); 
    bookmark.setDescription(form.getDescription()); 

    return bookmarkRepository.save(bookmark); 
} 
} 
+0

請顯示BookmarkService的代碼。 – dunni

+0

對不起,dunni。添加。 – douglas

回答

0

我是一個傻瓜。解決方案很簡單,我在dunni問我發佈BookmarkService後意識到了這一點。

BookmarkCreateForm(DTO)沒有User屬性。所以在BookmarkService中,當設置書籤對象的屬性時,用戶永遠不會被設置。顯然,JPA/Hibernate無法映射不存在的用戶。

代碼:

public class BookmarkCreateForm { 

@NotEmpty 
@URL 
private String url=""; 

private String description=""; 

private User user; 

public BookmarkCreateForm() { } 

//getters/setters 
} 

BookmarkService

@Service 
public class BookmarkServiceImpl implements BookmarkSerivce { 

private final BookmarkRepository bookmarkRepository; 

@Autowired 
public BookmarkServiceImpl(BookmarkRepository bookmarkRepository) { 
    this.bookmarkRepository = bookmarkRepository; 
} 

@Override 
public Bookmark create(BookmarkCreateForm form) { 
    Bookmark bookmark = new Bookmark(); 
    bookmark.setUrl(form.getUrl()); 
    bookmark.setDescription(form.getDescription()); 
    bookmark.setUser(form.getUser()); 

    return bookmarkRepository.save(bookmark); 
} 

}

BookmarkController

@RestController 
public class BookmarkController { 

private final UserService userService; 

private final BookmarkSerivce bookmarkSerivce; 

@Autowired 
public BookmarkController(UserService userService, BookmarkSerivce bookmarkSerivce) { 
    this.userService = userService; 
    this.bookmarkSerivce = bookmarkSerivce; 
} 

@RequestMapping("{userId}/add") 
public Bookmark addBookmark(@PathVariable("userId") Long userId, @RequestBody BookmarkCreateForm form) { 

    User user = userService.getUserById(userId) 
      .orElseThrow(() -> new NoSuchElementException(String.format("User=%s not found", userId))); 

    form.setUser(user); 
    Bookmark bookmark = bookmarkSerivce.create(form); 

    return bookmark; 
} 

}