many-to-many 和 one-to-many 在 Java Spring Boot 中的映射。

huangapple go评论98阅读模式
英文:

many-to-many and one-to-many mapping in java springboot

问题

以下是您提供的代码的翻译部分:

User(用户)类

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @ToString
  6. @Entity
  7. @Table(name = "user")
  8. public class User {
  9. @Id
  10. @GeneratedValue(strategy = GenerationType.IDENTITY)
  11. @Column(name="user_Id")
  12. private int userId;
  13. @Column(name="name")
  14. private String name;
  15. @Column(name="lastname")
  16. private String lastname;
  17. @Column(name="email")
  18. private String email;
  19. @Column(name="password")
  20. private String password;
  21. @Column(name="isActive")
  22. private boolean isActive;
  23. @Column(name="lastActive")
  24. private String lastActive;
  25. @Column(name="createdDate")
  26. private String createdDate;
  27. @Column(name="isBlocked")
  28. private boolean isBlocked;
  29. @ManyToOne(fetch = FetchType.LAZY, optional = false)
  30. @JoinColumn(name = "institution_id", nullable = false)
  31. private Institution institution;
  32. @ManyToMany(mappedBy = "users")
  33. private Set<Role> roles;
  34. }

Role(角色)类

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @ToString
  6. @Entity
  7. @Table(name = "role")
  8. public class Role {
  9. @Id
  10. @GeneratedValue
  11. @Column(name="role_Id")
  12. private int roleId;
  13. @Column(name="name")
  14. private String name;
  15. @Column(name="description")
  16. private String description;
  17. @ManyToMany(cascade = CascadeType.ALL)
  18. @JoinTable
  19. private Set<User> users;
  20. }

Institution(机构)类

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @ToString
  6. @Entity
  7. @Table(name = "institution")
  8. public class Institution {
  9. @Id
  10. @GeneratedValue
  11. @Column(name="institution_Id")
  12. private int institutionId;
  13. @Column(name="name")
  14. private String name;
  15. @Column(name="type")
  16. private String type;
  17. @Column(name="location")
  18. private String location;
  19. @OneToMany(mappedBy = "institution", fetch = FetchType.LAZY)
  20. private Set<User> user;
  21. }

关于您的问题,代码看起来大致正确。然而,在您添加了Many-to-Many关联后,未能创建表格可能是因为Hibernate在创建数据库表格时遇到了问题。请尝试以下步骤:

  1. 确保数据库已创建:确保在MySQL Workbench中创建了名为"mydb"的数据库。

  2. 检查依赖:确保您的项目中包含了正确的Hibernate和Spring Data JPA依赖项。检查项目的pom.xml或gradle.build文件以确保依赖正确配置。

  3. 查看错误日志:运行项目时,查看控制台或日志文件以查看是否有任何与数据库表创建相关的错误消息。这可能会提供有关问题的更多信息。

  4. 检查Many-to-Many配置:确保Many-to-Many关联正确配置。在Role和User实体类中,Many-to-Many注解和JoinTable注解似乎不完整。确保这些注解的配置正确,并且关联字段的名称是唯一的。

如果问题仍然存在,请提供任何错误消息或详细信息,以便我能够提供更具体的帮助。

英文:

I have those 3 entities that I want to transform to tables with relations

------user------

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @ToString
  6. @Entity
  7. @Table(name = &quot;user&quot;)
  8. public class User {
  9. @Id
  10. @GeneratedValue(strategy = GenerationType.IDENTITY)
  11. @Column(name=&quot;user_Id&quot;)
  12. private int userId;
  13. @Column(name=&quot;name&quot;)
  14. private String name;
  15. @Column(name=&quot;lastname&quot;)
  16. private String lastname;
  17. @Column(name=&quot;email&quot;)
  18. private String email;
  19. @Column(name=&quot;password&quot;)
  20. private String password;
  21. @Column(name=&quot;isActive&quot;)
  22. private boolean isActive;
  23. @Column(name=&quot;lastActive&quot;)
  24. private String lastActive;
  25. @Column(name=&quot;createdDate&quot;)
  26. private String createdDate;
  27. @Column(name=&quot;isBlocked&quot;)
  28. private boolean isBlocked;
  29. @ManyToOne(fetch = FetchType.LAZY, optional = false)
  30. @JoinColumn(name = &quot;institution_id&quot;, nullable = false)
  31. private Institution institution;
  32. @ManyToMany(mappedBy = &quot;users&quot;)
  33. private Set&lt;Role&gt; roles;
  34. }

-----role------

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @ToString
  6. @Entity
  7. @Table(name = &quot;role&quot;)
  8. public class Role {
  9. @Id
  10. @GeneratedValue
  11. @Column(name=&quot;role_Id&quot;)
  12. private int roleId;
  13. @Column(name=&quot;name&quot;)
  14. private String name;
  15. @Column(name=&quot;description&quot;)
  16. private String description;
  17. @ManyToMany(cascade = CascadeType.ALL)
  18. @JoinTable
  19. private Set&lt;User&gt; users;
  20. }

-----institution------

  1. @Getter
  2. @Setter
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @ToString
  6. @Entity
  7. @Table(name = &quot;institution&quot;)
  8. public class Institution {
  9. @Id
  10. @GeneratedValue
  11. @Column(name=&quot;institution_Id&quot;)
  12. private int institutionId;
  13. @Column(name=&quot;name&quot;)
  14. private String name;
  15. @Column(name=&quot;type&quot;)
  16. private String type;
  17. @Column(name=&quot;location&quot;)
  18. private String location;
  19. @OneToMany(mappedBy = &quot;institution&quot;, fetch = FetchType.LAZY)
  20. private Set&lt;User&gt; user;
  21. }

But when I run my project as a java application, no tables are created in mySQL workbench (the error occurred after adding he many-to-many annotation it was creating the tables before I add it).

Any ideas on why my code isn't working ?
And this is my application.properties

  1. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  2. spring.datasource.url = jdbc:mysql://localhost:3306/mydb
  3. spring.datasource.username = root
  4. spring.datasource.password = pass123
  5. spring.jpa.show-sql = true
  6. spring.jpa.hibernate.ddl-auto = update
  7. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  8. server.port=9090

答案1

得分: 1

@OneToMany 不会创建表格。
@ManyToMany 会创建一个表格,你需要指定它的细节,例如:

在用户类中:

  1. @ManyToMany
  2. @JoinTable(name = "users_roles",
  3. joinColumns = { @JoinColumn(name = "user_id") },
  4. inverseJoinColumns = { @JoinColumn(name = "role_id") })
  5. private Set<Role> roles;

在角色类中:

  1. @ManyToMany(mappedBy="roles")
  2. private Set<User> users;
英文:

@OneToMany does not create table.
@ManyToMany creates a table, you have to specify its details, like:

In user class:

  1. @ManyToMany
  2. @JoinTable(name = &quot;users_roles&quot;,
  3. joinColumns = { @JoinColumn(name = &quot;user_id&quot;) },
  4. inverseJoinColumns = { @JoinColumn(name = &quot;role_id&quot;) })
  5. private Set&lt;Role&gt; roles;

In role class:

  1. @ManyToMany(mappedBy=&quot;roles&quot;)
  2. private Set&lt;User&gt; users;

huangapple
  • 本文由 发表于 2020年8月12日 19:33:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63375633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定