All, I'm using hibernate with annotations and Spring and running into some issues...
My User class is annotated and basically has these attributes...
firstName, lastName, userName, etc... All are String types...
I'm using the Column annotation and annotating my userName attribute as @Id.
@Column (length=100, name="user_name")
@Id
public String getUserName() {
return userName;
}
I have a DAO class that extends HibernateDaoSupport and the insert method is relatively standard...
public void insertUser(User User) {
getHibernateTemplate().save(User);
}
I also have a service class UserService that has the following method...
public User addUser(User user) {
SpringBeanFactory spf = SpringBeanFactory.getInstance();
XmlBeanFactory factory = spf.getXmlBeanFactory();
UserDAO userDao = (UserDAO) factory.getBean("UserDao");
String userName = user.getUserName();
userDao.insertUser(user);
user = userDao.findUser(userName);
return user;
}
in the following test case...
UserService service = new UserService();
User user = new User();
user.setFirstName("John");
user.setLastName("Smith");
user.setUserName("jsmith");
user = service.addUser(user);
User user2 = new User();
user2.setFirstName("Bob");
user2.setLastName("Johnson");
user2.setUserName("bjohnson");
service.addUser(user2);
The first user is inserted, but the second user basically overwrites it. So it looks like I keep pesisting the same entity.
Is there something I'm missing? I wonder if this is some Spring/singleton issue, or maybe my configs are wrong?
Can someone please provide some hints?
Thanks.
Ilya
|