gpt4 book ai didi

java - 中号 :N table does not update

转载 作者:行者123 更新时间:2023-12-02 03:05:58 26 4
gpt4 key购买 nike

用户和文档之间有 m:n 关系。我正在创建 Document 对象,获取 List - setDocumentsForUsers(),然后保留该对象。问题是,该文档是在我的数据库中创建的,但不是 M:N 关系。我究竟做错了什么?我坚持后尝试调用flush,但根本没有帮助。

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "first_name")
private String firstName;
private String surname;
private String email;
private String password;

@ManyToMany
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(
name = "role_id", referencedColumnName = "id"))
private List<Role> roles;

@JsonIgnore
@ManyToMany
@JoinTable(
name="users_documents",
joinColumns = @JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(
name="document_id", referencedColumnName = "id"))
private List<Document> usersDocuments;


@OneToMany(mappedBy="user")
private List<Document> sharedDocuments;


public User() {

}

public User(String firstName, String surname, String email, String password) {
this.firstName = firstName;
this.surname = surname;
this.email = email;
this.password = password;
}

public void setId(long id) {
this.id = id;
}

public long getId() {
return id;
}

public String getEmail() {
return email;
}

public void setEmail(String email){this.email = email;}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public void setRoles(List<Role> roles) {
this.roles = roles;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public List<Role> getRoles() {
return roles;
}

public List<Document> getUsersDocuments() {
return usersDocuments;
}

public void setUsersDocuments(List<Document> usersDocuments) {
this.usersDocuments = usersDocuments;
}

public List<Document> getSharedDocuments() {
return sharedDocuments;
}

public void setSharedDocuments(List<Document> sharedDocuments) {
this.sharedDocuments = sharedDocuments;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return getId() == user.getId();
}

@Override
public int hashCode() {
return Objects.hash(getId());
}

@Override
public String toString() {
return "User{" +
"firstName='" + firstName + '\'' +
", surname='" + surname + '\'' +
", email='" + email + '\'' +
", roles=" + roles +
'}';
}
}

我的文档类:

@Entity
public class Document {

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

@Column(unique = true)
private String name;

private String title;

private String description;

@Column(name = "resource_path")
private String resourcePath;

@Column(name = "upload_datetime", columnDefinition = "DATETIME")
@Temporal(TemporalType.TIMESTAMP)
private Date uploadDatetime;

@ManyToMany(mappedBy = "usersDocuments")
private List<User> documentsForUsers;

@ManyToOne
@JoinColumn(name="user_id", nullable=false)
private User user;


public Document() {

}

public Document(String title, String description){
this.title = title;
this.description = description;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getResourcePath() {
return resourcePath;
}

public void setResourcePath(String resourcePath) {
this.resourcePath = resourcePath;
}


@Override
public String toString() {
return "Document{" +
"id=" + id +
", title='" + title + '\'' +
", description='" + description + '\'' +
", resourcePath='" + resourcePath + '\'' +
", uploadDatetime=" + uploadDatetime + '\'' +
". user=" + user;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Document)) return false;
Document document = (Document) o;
return getId() == document.getId();
}

@Override
public int hashCode() {
return Objects.hash(getId());
}

public Date getUploadDatetime() {
return uploadDatetime;
}

public void setUploadDatetime(Date uploadDatetime) {
// Date startDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(uploadDatetime.toString());
this.uploadDatetime = uploadDatetime;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public List<User> getDocumentsForUsers() {
return documentsForUsers;
}

public void setDocumentsForUsers(List<User> documentsForUsers) {
this.documentsForUsers = documentsForUsers;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

执行此操作:

Optional<User> user = userService.getUserByEmail(createdBy);
Document document = new Document(title, desc);
document.setUploadDatetime(new Date());
document.setUser(user.get());

List<User> users = userService.getUsersByRoles(roles);

document.setDocumentsForUsers(users);
saveDocument(document);


@Override
public void saveDocument(Document document) {
entityManager.persist(document);
}

最佳答案

我认为您需要将选项添加到 @ManyToMany 注释中:CascadeType.PERSIST。

默认情况下,hibernate 不会保留您的关系对象。

尝试使用:

@ManyToMany(cascade = CascadeType.PERSIST)

关于java - 中号 :N table does not update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57022924/

26 4 0