我在Spring Boot
项目中与JPA
联系。我坚持many-to-many
和users
之间的映射roles
关系。我使用持久性工具根据模式表生成java类。
表USERS的Sql脚本:
CREATE TABLE "MPOSML"."USERS"
( "USER_ID" VARCHAR2(32 BYTE),
"USER_NAME" VARCHAR2(64 BYTE),
"PASSWORD" VARCHAR2(64 BYTE),
"BRANCH_ID" VARCHAR2(6 BYTE),
"GROUP_ID" VARCHAR2(16 BYTE),
"ENABLED" VARCHAR2(5 BYTE),
"CREATE_BY" VARCHAR2(16 BYTE),
"CREATED_ON" DATE,
"MODIFIED_BY" VARCHAR2(16 BYTE),
"MODIFIED_ON" DATE,
"VERSION_NUMBER" NUMBER(4,0),
"PREFERED_LOCALE" VARCHAR2(5 BYTE) DEFAULT 'en',
"PREFERED_THEME" VARCHAR2(20 BYTE),
"FAILED_ATTEMPTS" NUMBER(3,0) DEFAULT 0,
"CLIENT_ID" VARCHAR2(16 BYTE),
"TEL" VARCHAR2(30 BYTE),
"EMAIL" VARCHAR2(120 BYTE),
"C_SMS" VARCHAR2(5 BYTE) DEFAULT 'false',
"C_IDEVICE" VARCHAR2(5 BYTE) DEFAULT 'false',
"C_WEB" VARCHAR2(5 BYTE) DEFAULT 'false',
"C_ANDROID" VARCHAR2(5 BYTE) DEFAULT 'false',
"DEVICE_CODE" VARCHAR2(2000 BYTE) DEFAULT 'WEB',
"ISAUTHORIZE" VARCHAR2(5 BYTE),
CONSTRAINT "USERS_ENABLED_CHECK" CHECK ("ENABLED" IS NOT NULL) ENABLE NOVALIDATE,
CONSTRAINT "USERS_GROUP_ID_CHECK" CHECK ("GROUP_ID" IS NOT NULL) ENABLE NOVALIDATE,
CONSTRAINT "USERS_PASSWORD_CHECK" CHECK ("PASSWORD" IS NOT NULL) ENABLE NOVALIDATE,
CONSTRAINT "USERS_USER_NAME_CHECK" CHECK ("USER_NAME" IS NOT NULL) ENABLE NOVALIDATE,
CONSTRAINT "PK_USERS" PRIMARY KEY ("USER_ID", "CLIENT_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MPOS" ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 3145728 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MPOS" ;
sql for table ROLES
CREATE TABLE "MPOSML"."ROLES"
( "ROLE_ID" VARCHAR2(16 BYTE),
"ROLE_NAME" VARCHAR2(64 BYTE),
"ENABLED" VARCHAR2(5 BYTE),
"CREATED_BY" VARCHAR2(16 BYTE),
"CREATED_ON" DATE,
"MODIFIED_BY" VARCHAR2(16 BYTE),
"MODIFIED_ON" DATE,
"VERSION_NUMBER" NUMBER(4,0),
"ROLE_GROUP" VARCHAR2(20 BYTE),
CONSTRAINT "ROLES_ENABLED_CHECK" CHECK ("ENABLED" IS NOT NULL) ENABLE NOVALIDATE,
CONSTRAINT "ROLES_ROLE_NAME_CHECK" CHECK ("ROLE_NAME" IS NOT NULL) ENABLE NOVALIDATE,
CONSTRAINT "ROLES_UNIQUE_KEY" UNIQUE ("ROLE_NAME")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 2097152 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MPOS" ENABLE,
CONSTRAINT "PK_ROLES" PRIMARY KEY ("ROLE_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 2097152 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MPOS" ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 3145728 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MPOS" ;
表格USERS_ROLES的SQL
CREATE TABLE "MPOSML"."USERS_ROLES"
( "USER_ID" VARCHAR2(16 BYTE),
"ROLE_ID" VARCHAR2(16 BYTE),
"SELECTED" VARCHAR2(5 BYTE),
"CREATED_BY" VARCHAR2(16 BYTE),
"CREATED_ON" DATE,
"MODIFIED_BY" VARCHAR2(16 BYTE),
"MODIFIED_ON" DATE,
"VERSION_NUMBER" NUMBER(4,0),
"CLIENT_ID" VARCHAR2(16 BYTE),
CONSTRAINT "USERSROLES_PRIMARY_KEY" PRIMARY KEY ("CLIENT_ID", "USER_ID", "ROLE_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 131072 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MPOS" ENABLE,
CONSTRAINT "FK_USERSROLES_ROLE_ID" FOREIGN KEY ("ROLE_ID")
REFERENCES "MPOSML"."ROLES" ("ROLE_ID") ON DELETE CASCADE ENABLE NOVALIDATE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 3145728 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "MPOS" ;
然后,在生成java类之后,它包含3个文件:Roles,Users和UsersPK Class UsersPK
public class UsersPK implements Serializable {
private String userId;
private String clientId;
@Column(name = "USER_ID")
@Id
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@Column(name = "CLIENT_ID")
@Id
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
}
类用户
@Entity
@IdClass(UsersPK.class)
public class Users {
private String userId;
private String userName;
private String clientId;
@ManyToMany
private Set<Roles> roles;
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id")
)
public Set<Roles> getRoles() {
return roles;
}
public void setRoles(Set<Roles> roles) {
this.roles = roles;
}
//getter and setter
}
类角色
@Entity
public class Roles {
private String roleId;
private String roleName;
private String enabled;
private String createdBy;
private Time createdOn;
private String modifiedBy;
private Time modifiedOn;
private Long versionNumber;
private String roleGroup;
@ManyToMany(mappedBy = "roles")
private Set<Users> users;
//getter & setter
}
在generate java class
之后,我为many-to-many relationship
添加了2 classes Users and Roles
。然后,当我运行项目时,控制台日志显示:
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: users, for columns: [org.hibernate.mapping.Column(roles)]
我在一些文章中进行了研究,发现他们的映射与我的相同,不知道这个问题会发生什么。
任何解决方案将不胜感激。谢谢和问候。