-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Entities are not being inserted, rather updated
PostPosted: Fri May 05, 2006 4:01 pm 
Newbie

Joined: Fri May 05, 2006 3:52 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 6:08 pm 
Beginner
Beginner

Joined: Mon Dec 06, 2004 4:20 pm
Posts: 34
Perhaps you need to specify the unsaved-value="null" property? Otherwise, Hibernate thinks you want to update.... not sure if this is your situation.

Don


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.