我有一个问题,就是如何在拥有多对一关系的情况下用Java保存数据库中的对象。
基本上我有两个类 - UserVO和GroupVO,看起来像这样:
public class UserVO extends ValueObject implements Serializable {
/**
* The default serial version ID
*/
private static final long serialVersionUID = 1L;
private String login;
private String password;
private Long groupId;
public UserVO() {
super();
this.setLogin("");
this.setPassword("");
this.setGroupId(0L);
}
// all the getters and setters
// ...
}
和
public final class GroupVO extends ValueObject implements Serializable {
/**
* The default serial version ID
*/
private static final long serialVersionUID = 1L;
private String description;
private Set<UserVO> users = new HashSet<UserVO>();
public GroupVO() {
super();
this.setDescription("");
}
// all the getters and setters
// ...
}
他们的超类是一个非常简单的抽象类:
public abstract class ValueObject {
private Long id;
private String name;
public ValueObject() {
super();
// the ID is auto-generated
// this.setId(0L);
this.setName("");
}
// all the getters and setters
// ...
}
现在我必须为它们创建DAO类。在UserDAO我是这样的,用于在DB中创建和插入用户:
@Override
public Long create(UserVO user) throws IllegalArgumentException, DAOException {
if (user.getId() != null) {
throw new IllegalArgumentException("User may already be created, the user ID is not null.");
}
Object[] values = { user.getName(), user.getLogin(), user.getPassword(), user.getGroupId() };
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet generatedKeys = null;
try {
connection = daoFactory.getConnection();
preparedStatement = DAOUtil.prepareStatement(connection, SQL_CREATE_USER, true, values);
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0) {
throw new DAOException("Creating user failed, no rows affected.");
}
generatedKeys = preparedStatement.getGeneratedKeys();
if (generatedKeys.next()) {
user.setId(generatedKeys.getLong(1));
} else {
throw new DAOException("Creating user failed, no generated key obtained.");
}
} catch (SQLException e) {
throw new DAOException(e);
} finally {
DAOUtil.close(connection, preparedStatement, generatedKeys);
}
return user.getId();
}
还有一些辅助类,但我想你理解我的代码:)。
这是GroupDAO创建方法:
@Override
public Long create(GroupVO group) throws IllegalArgumentException, DAOException {
if (group.getId() != null) {
throw new IllegalArgumentException("Group may already be created, the group ID is not null.");
}
Object[] values = { group.getName(), group.getDescription() };
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet generatedKeys = null;
try {
connection = daoFactory.getConnection();
preparedStatement = DAOUtil.prepareStatement(connection, SQL_CREATE_GROUP, true, values);
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows == 0) {
throw new DAOException("Creating group failed, no rows affected.");
}
generatedKeys = preparedStatement.getGeneratedKeys();
if (generatedKeys.next()) {
group.setId(generatedKeys.getLong(1));
} else {
throw new DAOException("Creating group failed, no generated key obtained.");
}
} catch (SQLException e) {
throw new DAOException(e);
} finally {
DAOUtil.close(connection, preparedStatement, generatedKeys);
}
return group.getId();
}
现在,如果我在main函数中进行一个小测试,创建一个组并保存在DB中,一切顺利:
DAOFactory usersGroupsRolesFactory = DAOFactory.getInstance("UsersGroupsRolesDB.jdbc");
System.out.println("DAOFactory successfully obtained: " + usersGroupsRolesFactory);
// Create an instance of the GroupDAO class
GroupDAO dao = usersGroupsRolesFactory.getGroupDAO();
// Create some GroupVO objects
GroupVO group1 = new GroupVO();
group1.setName("Administrators");
group1.setDescription("These users have all the right in the application");
dao.create(group1);
由于GroupVO类有一组UserVO对象,而在main函数中如果我也输入:
UserVO user1 = new UserVO();
user1.setName("Palancica Pavel");
user1.setLogin("login_pavel");
user1.setPassword("password_pavel");
group1.getUsers().add(user1); // I may also add some more users
并说我第一次打电话:dao.create(group1);
通常,这不仅应保存组信息,还应保存所有关联的UserVO对象。
对我而言,这意味着在GroupDAO的“创建”功能中,成功生成组ID后,我需要做很多其他代码。
我想知道这是在DB中保存这些用户的正确方法,因为我认为我必须让GroupDAO类与UserDAO类进行通信,并且还要使用DAOFactory,在我的例子中,它可以给我们UserDAO或GroupDAO对象。或者我可以在不使用UserDAO类的情况下进行所有数据库交互以保存这些用户。
我正在考虑的那段代码似乎很长,凌乱/意大利面,我不太确定这是否是正确的方法:(。
请注意,我注意到使用任何ORM框架。
请让我知道伙计们,你怎么看?! 如果您需要更多详细信息,我可以将您的项目发送给您,这不是商业广告:D
提前致谢
答案 0 :(得分:2)
我将使用GroupDAO仅创建组,UserDAO仅创建用户,并使用功能服务委托这两个DAO创建一个包含其所有用户的组。它的代码如下所示:
Long groupId = groupDao.createGroup(groupVO);
for (UserVO userVO : groupVO.getUsers()) {
userDao.createUser(userVO, groupId);
}