5

When we use the @ManyToMany annotation to run the background, an association table will be automatically generated in the database. For example, we have the following two entities, and the relationship between them is ManyToMany

 public class Task{
  . . .
  @ApiModelProperty("任务表单")
  @ManyToMany()
  private List<FormItem> formItems = new ArrayList<>();
}

public class FormItem{
  . . .
  @ApiModelProperty("任务表单")
  @ManyToMany()
  private List<Task> tasks = new ArrayList<>();
}

In this case, two intermediate tables, form_item_tasks and task_form-items, will be generated, and by default, if you maintain formItems on the task side, the data will only be automatically stored in the task_form_items table, and in turn, maintaining tasks in formItem will be the same as above— - Stored in form_item_tasks. That is, the previous item is the "main item".
If we want to generate only one table then we need to do the following:

 public class Task{
  . . .
  @ApiModelProperty("任务表单")
  @ManyToMany()
  @JoinTable(name = Task.TABLE_NAME + "_" + FormItem.TABLE_NAME,
          joinColumns = @JoinColumn(name = FormItem.TABLE_NAME + "_id"),
          inverseJoinColumns = @JoinColumn(name = Task.TABLE_NAME + "_id")
  )
  private List<FormItem> formItems = new ArrayList<>();
}

public class FormItem{
  . . .
  @ApiModelProperty("任务表单")
  @ManyToMany()
  @JoinTable(name = Task.TABLE_NAME + "_" + FormItem.TABLE_NAME,
          joinColumns = @JoinColumn(name = Task.TABLE_NAME + "_id"),
          inverseJoinColumns = @JoinColumn(name = FormItem.TABLE_NAME + "_id")
  )
  private List<Task> tasks = new ArrayList<>();
}

This will only generate the table you declared in name in @JoinTable, and it will work fine no matter which side the maintenance is performed on.

Possible errors:

  1. Hibernate: alter table task_form_item add constraint FK74s28wmeq8dcs2ms0mnxkmueb foreign key (form_item_id) references form_item (id)
    图片.png
    The reason for this error may be due to improper operation of the data table, resulting in incomplete data, we only need to delete the existing task_form_item data table and then run it.

2.图片.png
After the translation, I found that we were prompted to repeat column for ... form_item_id, that is, we reused this field. When we go back to the code again, we will find that our joinColumns and inverseJoinColumns are assigned the same field.

 @ManyToMany()
  @JoinTable(name = Task.TABLE_NAME + "_" + FormItem.TABLE_NAME,
          joinColumns = @JoinColumn(name = FormItem.TABLE_NAME + "_id"),
          inverseJoinColumns = @JoinColumn(name = TABLE_NAME + "_id")
  )
  @ApiModelProperty("所属任务")
  private List<Task> tasks = new ArrayList<>();

And it is worth mentioning that the assignment of joinColumns and inverseJoinColumns has nothing to do with this entity, that is, the exchange of the two will not cause other effects.


In addition, a database error suddenly appeared during the execution of the background recently. After viewing the database, I found that the following error occurred again:

 1045 - access denied for user 'root'@'localhost' (using password: yes)

Since I encountered this problem before and wrote a related article , after following the previous method, I found that there were many other errors in the middle. Using mysql -u root -p can log in normally, and the test in the database shows that the connection is successful, but clicking on the database still reports the above error. In the end, I can only try to reinstall mysql. Since the online reinstallation tutorials are mixed, I will share a tutorial that is available for personal testing: ubuntu 20.04 completely deletes mysql and reinstalls mysql .
To sum up, there are many reasons for mysql to report errors. There may be online methods to solve most of these errors, but there will always be accidents. If the standard method fails, the quickest method is to uninstall and reinstall.

Guess the reason: the password expiration policy is the default, and the password expires, so an error will be reported when logging in to mysql

图片.png


李明
441 声望18 粉丝