锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
@JoinTable(name = "user_role",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public void addRole(Role role) {
if (!this.roles.contains(role)) {
this.roles.add(role);
}
}
public void removeRole(Role role) {
this.roles.remove(role);
}
Role涓殑閮ㄥ垎浠g爜濡備笅錛?br />
@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
mappedBy = "roles",
targetEntity = User.class
)
public Set<User> getUsers() {
return users;
}
鑰屾祴璇曚唬鐮佺戶鎵夸簡AbstractTransactionalJUnit4SpringContextTests錛屼唬鐮佸涓嬶細(xì)
@Test
public void testManyToMany() {
Role oneRole = new Role();
oneRole.setDescription("manager");
oneRole.setEnabled(true);
oneRole.setRoleName("manger");
Role twoRole = new Role();
twoRole.setDescription("waitress");
twoRole.setEnabled(true);
twoRole.setRoleName("waitress");
User user = new User();
user.setEnabled(true);
user.setPassword("jianghaiying");
user.setUsername("Jiang HaiYing");
user.addRole(oneRole);
user.addRole(twoRole);
userDAO.persist(user);
try {
userDAO.getConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
榪欐牱鎵ц浠ュ悗錛屾墦鍗板嚭鐨勪俊鎭涓嬶細(xì)
Hibernate: insert into user (enabled, password, username) values (?, ?, ?)
Hibernate: insert into role (description, enabled, name) values (?, ?, ?)
Hibernate: insert into role (description, enabled, name) values (?, ?, ?)
榪欐椂鍊欓棶棰樺嚭鏉ヤ簡錛屼負(fù)浠涔堟病鏈夊線鍏崇郴琛ㄤ腑鎻掑叆鏁版嵁錛?br />
鍏跺疄榪欏茍涓嶆槸浠g爜鎴栬呴厤緗啓閿欒浜嗭紝鍦ㄦ寮忚繍琛屼唬鐮佷竴鍒囨甯革紝鑰屾槸AbstractTransactionalJUnit4SpringContextTests鍑虹殑楝鹼紝浜嬪疄涓婂瀵瑰鍏寵仈鍏崇郴鏄敱Hibernate鍘誨府鎴戜滑緇存姢鐨勶紝鑰孉bstractTransactionalJUnit4SpringContextTests涓轟簡淇濇寔鏁版嵁鐨勬竻媧佸張浼?xì)鑷姩鍥炴粴銆傚浣曡В鍐寵繖涓棶棰樺憿錛?br />
鏂規(guī)硶錛?br />
鍙渶瑕佸湪test鏂規(guī)硶涓婃坊鍔?span style="color: #008000">@Rollback(false)錛?/span>涓嶈瀹冨洖婊氾紝涓鍒囨甯鎬簡銆傝繖鏃跺欎篃鍙互鍘繪帀try璇彞浜嗐?br />
Hibernate: insert into user (enabled, password, username) values (?, ?, ?)
Hibernate: insert into role (description, enabled, name) values (?, ?, ?)
Hibernate: insert into role (description, enabled, name) values (?, ?, ?)
Hibernate: insert into user_role (user_id, role_id) values (?, ?)
Hibernate: insert into user_role (user_id, role_id) values (?, ?)
]]>
import Java.text.SimpleDateFormat;
import Java.util.TimeZone;
import org.hibernate.SessionFactory;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
/**
* This class is the base class of all the tests,
* we can use the dependency injection functionality of spring in all the tests,
* and the default transaction mode is rollback, so we don't need to write special code to restore data after calling some methods affected database data.
*
* @author Rui Zhou, Copyright © 2008 foundersoftware. All Rights Reserved.
* @version 1.00, 2008-03-22 15:46
*/
public abstract class SpringTestCaseBase extends AbstractTransactionalDataSourceSpringContextTests {
protected SimpleDateFormat sdf;
public SpringTestCaseBase() {
// query the protected variables to implement denpendency injection automatically,
// so we don't need to write settor and gettor methods anymore.
this.setPopulateProtectedVariables(true);
sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getDefault());
}
protected String[] getConfigLocations() {
return new String[] { "file:WebRoot/WEB-INF/applicationContext*.xml"};
}
protected void flushSession(){
SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
sessionFactory.getCurrentSession().flush();
}
}