英文:
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在创建数据库表格时遇到了问题。请尝试以下步骤:
-
确保数据库已创建:确保在MySQL Workbench中创建了名为"mydb"的数据库。
-
检查依赖:确保您的项目中包含了正确的Hibernate和Spring Data JPA依赖项。检查项目的pom.xml或gradle.build文件以确保依赖正确配置。
-
查看错误日志:运行项目时,查看控制台或日志文件以查看是否有任何与数据库表创建相关的错误消息。这可能会提供有关问题的更多信息。
-
检查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 = "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;
}
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 = "users_roles",
joinColumns = { @JoinColumn(name = "user_id") },
inverseJoinColumns = { @JoinColumn(name = "role_id") })
private Set<Role> roles;
In role class:
@ManyToMany(mappedBy="roles")
private Set<User> users;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论