import java.io.Serializable;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-import javax.persistence.Version;
-
import org.wamblee.persistence.Persistent;
/**
* @author Erik Brakkee
*/
public abstract class AbstractPersistent implements Persistent {
-
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long primaryKey;
+
+ private Serializable primaryKey;
- @Version
- private int version;
+ private Number version;
protected AbstractPersistent() {
primaryKey = null;
* @see org.wamblee.persistence.Persistent#getPrimaryKey()
*/
@Override
- public Long getPrimaryKey() {
+ public Serializable getPrimaryKey() {
return primaryKey;
}
* org.wamblee.persistence.Persistent#setPrimaryKey(java.io.Serializable)
*/
@Override
- public void setPrimaryKey(Long aKey) {
+ public void setPrimaryKey(Serializable aKey) {
primaryKey = aKey;
}
*
* @see org.wamblee.persistence.Persistent#getPersistedVersion()
*/
- public int getPersistedVersion() {
+ public Number getPersistedVersion() {
return version;
}
*
* @see org.wamblee.persistence.Persistent#setPersistedVersion(int)
*/
- public void setPersistedVersion(int aVersion) {
+ public void setPersistedVersion(Number aVersion) {
version = aVersion;
}
}
*
*/
public Group(Group aGroup) {
+ primaryKey = aGroup.primaryKey;
+ version = aGroup.version;
name = aGroup.name;
}
import java.util.Set;
import java.util.TreeSet;
+import javax.persistence.CascadeType;
import javax.persistence.Entity;
+import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
*/
@Entity
@Table(name = "SEC_USER")
-public class User implements Serializable,
- Comparable {
-
+@NamedQueries( {
+ @NamedQuery(name = User.QUERY_FIND_BY_NAME, query = "select u from User u where u.name = :" +
+ User.NAME_PARAM),
+ @NamedQuery(name = User.QUERY_FIND_BY_GROUP_NAME, query = "select user from User user join user.groups grp where grp.name = :name"),
+ @NamedQuery(name = User.QUERY_COUNT_USERS, query = "select count(u) from User u"),
+ @NamedQuery(name = User.QUERY_ALL_USERS, query = "select u from User u")})
+public class User implements Serializable, Comparable {
+
+ public static final String QUERY_FIND_BY_NAME = "User.findByName";
+ public static final String QUERY_FIND_BY_GROUP_NAME = "User.findByGroupName";
+ public static final String QUERY_COUNT_USERS = "User.count";
+ public static final String QUERY_ALL_USERS = "User.all";
+ public static final String NAME_PARAM = "name";
+
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long primaryKey;
@Version
private int version;
-
+
/**
* User name.
*/
/**
* Groups the user belongs to.
*/
- @ManyToMany
+ @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
private Set<Group> groups;
/**
*
*/
public User(User aUser) {
+ primaryKey = aUser.primaryKey;
+ version = aUser.version;
name = aUser.name;
password = aUser.password;
groups = new TreeSet<Group>();
public int compareTo(Object aUser) {
return name.compareTo(((User) aUser).name);
}
-
+
public Long getPrimaryKey() {
return primaryKey;
}
*
* @param aUser
* User.
+ * @return The modified user. The user passed in to this call should be considered invalid.
*/
void userModified(User aUser);
* User to check for existence.
*
*/
- protected void checkUserExists(String aUser) throws SQLException {
+ protected void checkUserExists(String aUser) throws Exception {
// Empty
}
* User to check for non-existence.
*
*/
- protected void checkUserNotExists(String aUser) throws SQLException {
+ protected void checkUserNotExists(String aUser) throws Exception {
// Empty
}
* Expected number of users.
*
*/
- protected void checkUserCount(int aSize) throws SQLException {
+ protected void checkUserCount(int aSize) throws Exception {
assertEquals(aSize, users.size());
}
* User to check for existence.
*
*/
- protected void checkGroupExists(String aUser) throws SQLException {
+ protected void checkGroupExists(String aUser) throws Exception {
// Empty
}
* User to check for non-existence.
*
*/
- protected void checkGroupNotExists(String aUser) throws SQLException {
+ protected void checkGroupNotExists(String aUser) throws Exception {
// Empty
}
* Expected number of users.
*
*/
- protected void checkGroupCount(int aSize) throws SQLException {
+ protected void checkGroupCount(int aSize) throws Exception {
// Empty
}
* contains().
*
*/
- public void testAdd() throws SQLException, UserMgtException {
+ public void testAdd() throws Exception {
User user = createUser("user1", PASSWORD, group);
assertTrue(users.add(user));
checkUserExists(user.getName());
* Tries to find a non-existing user. Verifies that null is returned.
*
*/
- public void testFindUnknownUser() throws SQLException, UserMgtException {
+ public void testFindUnknownUser() throws Exception {
User user1 = createUser("user1", PASSWORD, group);
User user2 = createUser("user2", PASSWORD, group);
users.add(user1);
* Adds duplicate user. Verifies that the existing user is left untouched.
*
*/
- public void testAddDuplicateUser() throws SQLException, UserMgtException {
+ public void testAddDuplicateUser() throws Exception {
User user1 = createUser("user1", PASSWORD, group);
users.add(user1);
* true.
*
*/
- public void testRemoveUser() throws SQLException, UserMgtException {
+ public void testRemoveUser() throws Exception {
User user1 = createUser("user1", PASSWORD, group);
users.add(user1);
assertTrue(users.contains(user1));
* the return value is true.
*
*/
- public void testRemoveNonExistingUser() throws SQLException,
+ public void testRemoveNonExistingUser() throws Exception,
UserMgtException {
User user1 = createUser("user1", PASSWORD, group);
users.add(user1);
* all.
*
*/
- public void testList() throws SQLException, UserMgtException {
+ public void testList() throws Exception {
User user1 = createUser("user1", PASSWORD, group);
User user2 = createUser("user2", PASSWORD, group);
User user3 = createUser("user3", PASSWORD, group);
*
* @throws SQLException
*/
- public void testListByGroup() throws SQLException, UserMgtException {
+ public void testListByGroup() throws Exception {
Group group1 = new Group("group1");
Group group2 = new Group("group2");
Group group3 = new Group("group3");
*/
public void userModified(User aUser) {
assert aUser.getPrimaryKey() != null;
- //hibernateSupport.merge(aUser);
+ //hibernateSupport.merge(aUser)
cache.remove(aUser.getName());
setPasswordInfo(aUser);
cache.put(aUser.getName(), new User(aUser));
* @see org.wamblee.usermgt.InMemoryGroupSetTest#checkGroupCount(int)
*/
@Override
- protected void checkUserCount(int aSize) throws SQLException {
+ protected void checkUserCount(int aSize) throws Exception {
databaseTester.flush();
super.checkUserCount(aSize);
assertEquals(aSize, databaseTester.getTableSize(USER_TABLE));
import org.junit.After;
import org.junit.Before;
+import org.junit.Ignore;
import org.wamblee.support.persistence.JpaTester;
import org.wamblee.support.persistence.TransactionProxyFactory;
import org.wamblee.support.persistence.DatabaseUtils.JdbcUnitOfWork;
import org.wamblee.usermgt.GroupSet;
import org.wamblee.usermgt.InMemoryGroupSetTest;
+
public class JpaGroupSetTest extends InMemoryGroupSetTest {
private static final String GROUP_TABLE = "SEC_GROUP";
public void setUp() throws Exception {
jpaTester = new JpaTester(new SecurityPersistenceUnit());
jpaTester.start();
+
// Superclass setup will call createGroupSet so requires initialized JPA.
super.setUp();
}
GroupSet proxy = factory.getProxy(groupset);
return proxy;
}
-
}