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

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

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

问题

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

User(用户)类

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="user_Id")
    private int userId;
    @Column(name="name")
    private String name;
    @Column(name="lastname")
    private String lastname;
    @Column(name="email")
    private String email;
    @Column(name="password")
    private String password;
    @Column(name="isActive")
    private boolean isActive;
    @Column(name="lastActive")
    private String lastActive;
    @Column(name="createdDate")
    private String createdDate;
    @Column(name="isBlocked")
    private boolean isBlocked;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "institution_id", nullable = false)
    private Institution institution;

    @ManyToMany(mappedBy = "users")
    private Set<Role> roles;

}

Role(角色)类

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString

@Entity
@Table(name = "role")
public class Role {

    @Id
    @GeneratedValue
    @Column(name="role_Id")
    private int roleId;
    @Column(name="name")
    private String name;
    @Column(name="description")
    private String description;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable
    private Set<User> users;

}

Institution(机构)类

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString

@Entity
@Table(name = "institution")
public class Institution {
    @Id
    @GeneratedValue
    @Column(name="institution_Id")
    private int institutionId;
    @Column(name="name")
    private String name;
    @Column(name="type")
    private String type;
    @Column(name="location")
    private String location;
    @OneToMany(mappedBy = "institution", fetch = FetchType.LAZY)
    private Set<User> user;

}

关于您的问题,代码看起来大致正确。然而,在您添加了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------

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString

@Entity
@Table(name = &quot;user&quot;)
public class User {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name=&quot;user_Id&quot;)
	private int userId;
	@Column(name=&quot;name&quot;)
	private String name;
	@Column(name=&quot;lastname&quot;)
	private String lastname;
	@Column(name=&quot;email&quot;)
	private String email;
	@Column(name=&quot;password&quot;)
	private String password;
	@Column(name=&quot;isActive&quot;)
	private boolean isActive;
	@Column(name=&quot;lastActive&quot;)
	private String lastActive;
	@Column(name=&quot;createdDate&quot;)
	private String createdDate;
	@Column(name=&quot;isBlocked&quot;)
	private boolean isBlocked;
	
	@ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = &quot;institution_id&quot;, nullable = false)
    private Institution institution;
	
	@ManyToMany(mappedBy = &quot;users&quot;)
    private Set&lt;Role&gt; roles;

}

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

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString

@Entity
@Table(name = &quot;role&quot;)
public class Role {
	
	@Id
	@GeneratedValue
	@Column(name=&quot;role_Id&quot;)
	private int roleId;
	@Column(name=&quot;name&quot;)
	private String name;
	@Column(name=&quot;description&quot;)
	private String description;
	@ManyToMany(cascade = CascadeType.ALL)
    @JoinTable
    private Set&lt;User&gt; users;

}

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

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString

@Entity
@Table(name = &quot;institution&quot;)
public class Institution {
	@Id
	@GeneratedValue
	@Column(name=&quot;institution_Id&quot;)
	private int institutionId;
	@Column(name=&quot;name&quot;)
	private String name;
	@Column(name=&quot;type&quot;)
	private String type;
	@Column(name=&quot;location&quot;)
	private String location;
	@OneToMany(mappedBy = &quot;institution&quot;, fetch = FetchType.LAZY)
    private Set&lt;User&gt; user;
	
}

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

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

答案1

得分: 1

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

在用户类中:

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

在角色类中:

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

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

In user class:

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

In role class:

@ManyToMany(mappedBy=&quot;roles&quot;)
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:

确定