我有三个表Users,USER_ROLES和APP_USERS_USER_ROLES。为Users和User_roles表创建实体bean。在Dao对象中使用了EntityManager对象,我尝试使用User Object设置UserRoles。用户对象已创建。但是没有创建APP_USERS_USER_ROLES中的相应行。在输出中,您可以观察到找不到APP_USERS_USER_ROLES的SQL语句。
我做错了什么?
表:
create table USERS (
userid BIGINT NOT NULL AUTO_INCREMENT,
username VARCHAR(30) NOT NULL,
password VARCHAR(256) NOT NULL,
first_name VARCHAR(64) NOT NULL,
middle_name VARCHAR(64) default '',
last_name VARCHAR(64) NOT NULL,
email VARCHAR(64) NOT NULL,
is_active ENUM ("1", "0") default 1,
PRIMARY KEY PK_USERS (userid),
UNIQUE UK_USERS_USERNAME (username),
UNIQUE UK_USERS_EMAIL (email)
);
create table USER_ROLES (
roleid BIGINT NOT NULL AUTO_INCREMENT,
rolename VARCHAR(30) NOT NULL,
PRIMARY KEY PK_USER_ROLES (roleid),
UNIQUE UK_USER_ROLES (rolename)
);
CREATE TABLE APP_USERS_USER_ROLES (
app_userid BIGINT NOT NULL,
app_roleid BIGINT NOT NULL,
PRIMARY KEY PK_APP_USERS_USER_ROLES (app_userid, app_roleid),
CONSTRAINT FK_APP_USERS_USER_ROLES_USERID FOREIGN KEY (app_userid) REFERENCES USERS (userid),
CONSTRAINT FK_APP_USERS_USER_ROLES_USERROLE FOREIGN KEY (app_roleid) REFERENCES USER_ROLES (roleid)
);
实体豆:
@Entity
@Table(name="USERS")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long userid;
....
@ManyToMany(mappedBy = "users")
private Set<UserRoles> userRoles = new HashSet<UserRoles>();
# Respective setters / getters
}
@Entity
@Table(name="USER_ROLES")
public class UserRoles {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long roleid;
...
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "APP_USERS_USER_ROLES",
joinColumns = @JoinColumn(name = "APP_ROLEID"),
inverseJoinColumns = @JoinColumn(name = "APP_USERID")
)
private Set<User> users = new HashSet<User> ();
# Respective getters & setters
}
道实施:
@Repository
@Transactional
public class UserDaoImpl implements UserDao {
@PersistenceContext
EntityManager em;
...
@Override
public void addUser(User user) {
System.out.println("here " + user);
if (user.getIsActive() == null) {
user.setIsActive(YNFlag.Yes);
} else if (user.getIsActive().toString().equalsIgnoreCase("No")) {
em.remove(user);
throw new IllegalArgumentException("Cannot set isactive to 'No' while adding a new user");
}
String passwordEncr = PasswordEncoder.encode(user.getPassword());
user.setPassword(passwordEncr);
Set<UserRoles> userRoles = user.getUserRoles();
user.setUserRoles(userRoles);
em.persist(user);
Logger logger = loggerCtx.getLogger(this.getClass());
logger.info("User created: " + user);
}
...
}
输出:
2017-Oct-01_17:35:01.142 [http-nio-8080-exec-7] INFO c.r.t.springmvc.dao.impl.UserDaoImpl Entering method - addUser - with arguments - [{Userid: null, Username: dingdong2, First name: ASDF, Middle name: ASDF, Last name: ASDF, Role: ADMINISTRATOR, USER, Isactive: null, Password: asdfasdf, Email: asdf@rsa.com}]
here {Userid: null, Username: dingdong2, First name: ASDF, Middle name: ASDF, Last name: ASDF, Role: ADMINISTRATOR, USER, Isactive: null, Password: asdfasdf, Email: asdf@rsa.com}
Hibernate:
insert
into
USERS
(email, first_name, is_active, last_name, middle_name, password, username)
values
(?, ?, ?, ?, ?, ?, ?)
17:35:01.254 [http-nio-8080-exec-7] INFO c.r.t.springmvc.dao.impl.UserDaoImpl - User created: {Userid: 14, Username: dingdong2, First name: ASDF, Middle name: ASDF, Last name: ASDF, Role: ADMINISTRATOR, USER, Isactive: Yes, Password: 4B2968E588D0630838676CD2F3D5159BA28FF77823D3FBE7B6C49DE44406D78517FEEEFEAEDB9FD6F11439132D40CEBAA16497C40510C5FCBDBFC03E03C85CB4, Email: asdf@rsa.com}
2017-Oct-01_17:35:01.254 [http-nio-8080-exec-7] INFO c.r.t.springmvc.dao.impl.UserDaoImpl User created: {Userid: 14, Username: dingdong2, First name: ASDF, Middle name: ASDF, Last name: ASDF, Role: ADMINISTRATOR, USER, Isactive: Yes, Password: 4B2968E588D0630838676CD2F3D5159BA28FF77823D3FBE7B6C49DE44406D78517FEEEFEAEDB9FD6F11439132D40CEBAA16497C40510C5FCBDBFC03E03C85CB4, Email: asdf@rsa.com}
17:35:01.254 [http-nio-8080-exec-7] INFO c.r.t.springmvc.dao.impl.UserDaoImpl - execution(UserDao.addUser(..)): 112 ms