测试junit添加了一个新角色而不是更新(它不会更新).i使用spring,hibernate,maven,mysql.someone有一个想法吗?谢谢:
TestJunit.java:
.........
public class TestJunit {
@Test
public void UpdateRole() {
InterfAdminMetier metier = (InterfAdminMetier) context
.getBean("metier");
Role r = metier.getRole("ROLE_Test");
System.out.println("1 before update************************************"+r.getRoleName());
r.setRoleName("ROLE_TestUpdate");
metier.updateRole(r);
System.out.println("2 after update************************************"+metier.getRole("ROLE_Test").getRoleName());
assertTrue(metier.getRole("ROLE_TestUpdate").getRoleName().contentEquals("ROLE_TestUpdate"));
}
}
User.java:
........
//import org.hibernate.mapping.Set;
/*classe JavaBean:les propriétes ne sont accessible que par les gettters et les setters
il faut definir un constructeur sans parametres si non il ne sera pas defini par defaut si nous deffinision un
constructeur avec parametres*/
@Entity
@Table(name = "users")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
private String id;
private String nom;
private String prenom;
private String mail;
private String pw;
/*
* un utilisateur peut avoir plusieurs role comme l'admin qui a les roles
* admin, biblio, enseignant, etud un role peut etre attribuer a plusieurs
* utili comme roleEtudiant
*/
/* @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "roles_users", joinColumns = {
@JoinColumn(name = "idUser", nullable = false, updatable = true) },
inverseJoinColumns = { @JoinColumn(name = "roleName",
nullable = false, updatable = true) })
private Set<Role> roles = new HashSet<Role>(0);*/
@ManyToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinTable(name = "roles_users", joinColumns = { @JoinColumn(name = "idUser", referencedColumnName = "id", nullable = false, updatable = true) }, inverseJoinColumns = { @JoinColumn(name = "roleName", referencedColumnName = "roleName", nullable = false, updatable = true) })
private Set<Role> roles = new HashSet<Role>(0);
........................................
public User() {
super();
// TODO Auto-generated constructor stub
}
..........................................
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public Collection<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
...................................
}
Role.java:
............
@Entity
@Table(name = "roles")
public class Role implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(name = "roleName")
private String roleName;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
private Set<User> users = new HashSet<User>(0);
/*
* @ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles") private
* Collection<User> users;
*/
public Role() {
super();
// TODO Auto-generated constructor stub
}
public Role(String roleName) {
super();
this.roleName = roleName;
}
public Collection<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
}
BiblioDaoImplementation.java:
...........
public class BiblioDaoImplementation implements InterfaBiblioDao {
@PersistenceContext(unitName = "UP_Biblio")
private EntityManager em;
// ---------------------------------------user---------------------------------
@Override
public String addUser(User user) {
em.persist(user);
return user.getId();
}
@Override
public String removeUser(String id) {
User user = getUser(id);
em.remove(user);
return user.getId();
}
@Override
public String updateUser(User user) {
em.merge(user);
return user.getId();
}
@Override
public User getUser(String Id) {
return em.find(User.class, Id);
}
@Override
public void attrubierRoleToUser(Role role, String id) {
User u = em.find(User.class, id);
u.getRoles().add(role);
}
@Override
public List<User> listUsers() {
Query req = em
.createQuery("select user from User user");
return req.getResultList();
}
@Override
public List<User> listUserByRole(String roleName) {
Query req = em
.createQuery(" SELECT u FROM Role r JOIN r.users u WHERE r.roleName=:x");
req.setParameter("x", roleName);
return req.getResultList();
}
// ---------------------------------------role---------------------------------
@Override
public String addRole(Role role) {
em.persist(role);
return role.getRoleName();
}
@Override
public String removeRole(String roleName) {
Role role = em.find(Role.class, roleName);
em.remove(role);
return roleName;
}
@Override
public String updateRole(Role role) {
em.merge(r);
return role.getRoleName();
}
@Override
public Role getRole(String roleName) {
return em.find(Role.class, roleName);
}
@Override
public List<Role> listRoles() {
Query req = em.createQuery("select role from Role role");
return req.getResultList();
}
}
的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd ">
<persistence-unit name="UP_Biblio" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
的applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:s="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/biblioissteg"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="datasource"></property>
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
<property name="persistenceUnitName" value="UP_Biblio"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:annotation-config></context:annotation-config>
<bean id="dao" class="tn.issteg.biblio.dao.BiblioDaoImplementation"></bean>
<bean id="metier" class="tn.issteg.biblio.metier.BiblioMetierImpl">
<property name="dao" ref="dao"></property>
</bean>
</beans>
控制台: ................ 信息:org.springframework.security.config.http.DefaultFilterChainValidator - 检查您的配置是否可以访问登录URL'/ login' Hibernate:从角色role0_中选择role0_.roleName作为roleName2_0_,其中role0_.roleName =? 1月上升************************************ ROLE_Test Hibernate:从角色role0_中选择role0_.roleName作为roleName2_0_,其中role0_.roleName =? Hibernate:插入角色(roleName)值(?) Hibernate:从角色role0_中选择role0_.roleName作为roleName2_0_,其中role0_.roleName =? 2,2分了************************************ ROLE_Test Hibernate:从角色role0_中选择role0_.roleName作为roleName2_0_,其中role0_.roleName =?
答案 0 :(得分:1)
因为您正在更改角色名称,这是Role类的标识符,因此创建了一个新角色,如果您更改除标识符之外的任何内容,则会更新
答案 1 :(得分:0)
尝试使用update
代替merge
:
public String updateRole(Role role) {
em.update(role);
return role.getRoleName();
}