k.siva prasadreddy

Greenhorn
+ Follow
since Sep 10, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by k.siva prasadreddy

Hi,

I hope you are getting the problem because of whenever you are calling getCurrentSession() you are not getting already existing session, you are getting a new session.

One way of solving this problem is to do as follows:

// open session
session = sessionFactory.openSession();
//start transaction
txn = session.beginTransaction(0;
//do your work
//session.save(user);
//close transaction
txn.commit();
//close session
session.close();


Thanks,
K. Siva Prasad Reddy.
Hi,
I test the above mentioned two ways on hibernate3 version and both methods are working fine for me.

And also we can use the first method to delete multiple rows which have the same name.

Thanks,
K. Siva Prasad Reddy.
Hi,
If you want to get the saved object immediately you can use session.merge(object), which returns the inserted objected.

Thanks,
K. Siva Prasad Reddy.
Hi,
I hope the following example can help you.


public class Role
{

private Integer roleId;
private String name;
private Set<User> users;
//setters getters
}

public class User
{
private Integer userId;
private String userName;
//setters getters
}

<class name="User" table="Users">
<id name="userId" column="user_id">
<generator class="native"/>
</id>
<property name="userName" column="username"/>

</class>

<class name="Role" table="ROLES">
<id column="role_id" name="roleId">
<generator class="native"></generator>
</id>
<property name="name" column="role_name"></property>
<set name="users" table="users_roles">
<key column="role_id"></key>
<many-to-many column="user_id" class="User"/>
</set>
</class>


Query query = session.createQuery("from Role where roleId = ?");
query.setInteger(0, 1);
List<Role> roles = query.list();
if(roles!=null){
for (Role role : roles) {
Set<User> users = role.getUsers();
for (User user2 : users) {
System.out.println(user2);
}
}
}
Hi,
IF you want to delete a row you can follow one of the following ways:

#1:

txn=session.beginTransaction();
User user = session.get(User.class,1);
session.delete(user);
txn.commit();

#2:
txn=session.beginTransaction();
Query query = session.createQuery("delete from User where userId = ?");
query.setInteger(0, 1);
query.executeUpdate();
txn.commit();
txn.commit();