Java Spring引导中的映射理解问题
我刚开始用弹簧靴。 我有3个表User、UserNotes和NotesJava Spring引导中的映射理解问题,java,mysql,spring,spring-boot,Java,Mysql,Spring,Spring Boot,我刚开始用弹簧靴。 我有3个表User、UserNotes和Notes 用户(用户ID、电子邮件ID) UserNotes(UserID,NotesID) 注释(注释SID、标题、消息) 当我添加电子邮件时,用户ID必须自动生成。当我添加与用户对应的任何注释时,UserNotes表中的注释Id必须自动生成,并且必须添加到Notes表中。 在MySQL中,我将User表中的userID作为主键,UserNotes中的userID作为外键引用它。类似地,UserNotes中的NotesID作为主键,
在MySQL中,我将User表中的userID作为主键,UserNotes中的userID作为外键引用它。类似地,UserNotes中的NotesID作为主键,而Notes表中的NotesID作为外键 当我使用MySQL查询时,一切正常。但现在我想使用Spring数据JPA来实现这一点。但我在理解如何通过多个表映射这些关系时遇到了困难。我试过一对夫妻和多对夫妻的关系,但都没有成功
MySQL Queries for the reference
1) Create table User(userID int AUTO_INCREMENT, emailID varchar(40), PRIMARY KEY(userID));
2) Create table UserNotes(userID int, NotesID int AUTO_INCREMENT, PRIMARY KEY(NotesID), Foreign key(UserID) references User(UserID) on DELETE CASCADE);
3) Create table Notes(notesID int, title varchar(100), message varchar(500), Date date, PRIMARY KEY(notesID), FOREIGN KEY(notesID) references UserNotes(NotesID) on DELETE CASCADE);
使用Hibernate、JPA、Lombok的未经测试的示例: 用户实体
@Entity
@Table(name = "USER")
@SequenceGenerator(name = "userSeqId", sequenceName = "user_seq_id", allocationSize = 1)
@NoArgsConstructor
@Setter
@Getter
public class User {
@Id
@GeneratedValue(generator = "userSeqId", strategy = GenerationType.AUTO)
private Long id;
@NotBlank
@Column(name = "EMAIL", unique = true)
private String email;
}
@Entity
@Table(name = "NOTES")
@SequenceGenerator(name = "notesSeqId", sequenceName = "notes_seq_id", allocationSize = 1)
@NoArgsConstructor
@Setter
@Getter
public class Notes {
@Id
@GeneratedValue(generator = "notesSeqId", strategy = GenerationType.AUTO)
private Long id;
@NotBlank
@Column(name = "TITLE")
private String title;
@NotBlank
@Column(name = "MESSAGE")
private String message;
}
@Entity
@Table(name = "USER_NOTES")
@NoArgsConstructor
@Setter
@Getter
public class UserNotes {
@EmbeddedId
private UserNotesKey id;
@NotNull
@ManyToOne
@MapsId("USER_ID")
@JoinColumn(name = "USER_ID")
@OnDelete(action = OnDeleteAction.CASCADE)
private User user;
@NotNull
@ManyToOne(cascade = CascadeType.PERSIST)
@MapsId("NOTES_ID")
@JoinColumn(name = "NOTES_ID")
@OnDelete(action = OnDeleteAction.CASCADE)
private Notes notes;
}
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@EqualsAndHashCode
public class UserNotesKey implements Serializable {
@Column(name = "USER_ID")
private Long userId;
@Column(name = "NOTES_ID")
private Long notesId;
}
注释实体
@Entity
@Table(name = "USER")
@SequenceGenerator(name = "userSeqId", sequenceName = "user_seq_id", allocationSize = 1)
@NoArgsConstructor
@Setter
@Getter
public class User {
@Id
@GeneratedValue(generator = "userSeqId", strategy = GenerationType.AUTO)
private Long id;
@NotBlank
@Column(name = "EMAIL", unique = true)
private String email;
}
@Entity
@Table(name = "NOTES")
@SequenceGenerator(name = "notesSeqId", sequenceName = "notes_seq_id", allocationSize = 1)
@NoArgsConstructor
@Setter
@Getter
public class Notes {
@Id
@GeneratedValue(generator = "notesSeqId", strategy = GenerationType.AUTO)
private Long id;
@NotBlank
@Column(name = "TITLE")
private String title;
@NotBlank
@Column(name = "MESSAGE")
private String message;
}
@Entity
@Table(name = "USER_NOTES")
@NoArgsConstructor
@Setter
@Getter
public class UserNotes {
@EmbeddedId
private UserNotesKey id;
@NotNull
@ManyToOne
@MapsId("USER_ID")
@JoinColumn(name = "USER_ID")
@OnDelete(action = OnDeleteAction.CASCADE)
private User user;
@NotNull
@ManyToOne(cascade = CascadeType.PERSIST)
@MapsId("NOTES_ID")
@JoinColumn(name = "NOTES_ID")
@OnDelete(action = OnDeleteAction.CASCADE)
private Notes notes;
}
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@EqualsAndHashCode
public class UserNotesKey implements Serializable {
@Column(name = "USER_ID")
private Long userId;
@Column(name = "NOTES_ID")
private Long notesId;
}
UserNotes实体
@Entity
@Table(name = "USER")
@SequenceGenerator(name = "userSeqId", sequenceName = "user_seq_id", allocationSize = 1)
@NoArgsConstructor
@Setter
@Getter
public class User {
@Id
@GeneratedValue(generator = "userSeqId", strategy = GenerationType.AUTO)
private Long id;
@NotBlank
@Column(name = "EMAIL", unique = true)
private String email;
}
@Entity
@Table(name = "NOTES")
@SequenceGenerator(name = "notesSeqId", sequenceName = "notes_seq_id", allocationSize = 1)
@NoArgsConstructor
@Setter
@Getter
public class Notes {
@Id
@GeneratedValue(generator = "notesSeqId", strategy = GenerationType.AUTO)
private Long id;
@NotBlank
@Column(name = "TITLE")
private String title;
@NotBlank
@Column(name = "MESSAGE")
private String message;
}
@Entity
@Table(name = "USER_NOTES")
@NoArgsConstructor
@Setter
@Getter
public class UserNotes {
@EmbeddedId
private UserNotesKey id;
@NotNull
@ManyToOne
@MapsId("USER_ID")
@JoinColumn(name = "USER_ID")
@OnDelete(action = OnDeleteAction.CASCADE)
private User user;
@NotNull
@ManyToOne(cascade = CascadeType.PERSIST)
@MapsId("NOTES_ID")
@JoinColumn(name = "NOTES_ID")
@OnDelete(action = OnDeleteAction.CASCADE)
private Notes notes;
}
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@EqualsAndHashCode
public class UserNotesKey implements Serializable {
@Column(name = "USER_ID")
private Long userId;
@Column(name = "NOTES_ID")
private Long notesId;
}
存储库
public interface UserRepository extends JpaRepository<User, Long> {
}
public interface NotesRepository extends JpaRepository<Notes, Long> {
}
public interface UserNotesRepository extends JpaRepository<UserNotes, UserNotesKey> {
List<UserNotes> findByUser(User user);
}
public interface UserRepository扩展了JpaRepository{
}
公共接口NotesRepository扩展了JpaRepository{
}
公共接口UserNotesRepository扩展了JpaRepository{
列出findByUser(用户);
}
测试服务
@Service
@Transactional
@RequiredArgsConstructor
public class TestService {
private final UserRepository userRepository;
private final UserNotesRepository userNotesRepository;
public User saveUser(User user) {
User newUser = new User();
user.setEmail(user.getEmail());
return userRepository.save(user);
}
public UserNotes saveNotes(User user, Notes notes) {
UserNotes userNotes = new UserNotes();
userNotes.setUser(user);
userNotes.setNotes(notes);
return userNotesRepository.save(userNotes);
}
public List<Notes> getNotes(User user) {
return userNotesRepository.findByUser(user)
.stream()
.map(UserNotes::getNotes)
.collect(Collectors.toList());
}
}
@服务
@交易的
@所需参数构造函数
公共类测试服务{
私有最终用户存储库用户存储库;
私人最终用户备注保留用户备注保留;
公共用户saveUser(用户用户){
User newUser=新用户();
user.setEmail(user.getEmail());
返回userRepository.save(用户);
}
public UserNotes saveNotes(用户用户,Notes){
UserNotes UserNotes=newusernotes();
userNotes.setUser(用户);
userNotes.setNotes(notes);
返回userNotesRepository.save(userNotes);
}
公共列表getNotes(用户){
return userNotesRepository.findByUser(用户)
.stream()
.map(UserNotes::getNotes)
.collect(Collectors.toList());
}
}
您应该了解不同类型的实体关系,特别是许多关系。连接表UserNote不会生成任何内容,因为它们是它包含的外键,必须首先在相应的表中创建它们。一般来说,我建议您阅读一本关于sql的书,因为您需要了解一些基础知识。