-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate update child collection
PostPosted: Mon Jan 05, 2015 1:20 pm 
Newbie

Joined: Mon Jan 05, 2015 10:03 am
Posts: 3
I have a parent class 'Task' and it has child 'Tag' in it. where i want tag name(see Tag class) to be unique, but when i am trying to saving parent class a new child record is creating. How can i eliminate duplicates child records.

Here is my code

Task Class

Code:
@Entity
    @Table(name="tasks")
    public class TaskDTO {

    ...
    ...

    @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinTable( name = "task_tags",joinColumns = { @JoinColumn(name = "taskId") }, inverseJoinColumns = { @JoinColumn(name = "tagId") })
        private List<TagDTO> tags = new ArrayList<TagDTO>();


}


Tag Class

Code:
@Entity
@Table(name="tags")
public class TagDTO {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "tagId")
    private int tagId;

    @Column(name = "tagName")
    private String tagName;
}


TaskDAOImpl

Code:
@Repository("taskDAOImpl")
public class TaskDAOImpl implements TaskDAO {

@Override
    public Integer saveTask(TaskDTO taskDTO) {
        Integer identifier = null;
        try {


            Session session = sessionFactory.getCurrentSession();
            session.beginTransaction();

            ..........
            ..........
            identifier = (Integer) session.save(taskDTO);

            return identifier;
        } catch (Exception ex) {
            logger.error("unable to save task ", ex);
        }
        return null;
    }


When i saved tags i get this in my database.

Code:
mysql> select * from tags;
+-------+---------+
| tagId | tagName |
+-------+---------+
|     1 | java    |
|     2 | java    |
+-------+---------+
2 rows in set (0.00 sec)

mysql> select * from task_tags;
+--------+-------+
| taskId | tagId |
+--------+-------+
|     69 |     1 |
|     70 |     2 |
+--------+-------+
2 rows in set (0.00 sec)


i want to eliminate that tagName saved two times.

But i want some thing like this

Code:
mysql> select * from tags;
    +-------+---------+
    | tagId | tagName |
    +-------+---------+
    |     1 | java    |
    +-------+---------+
    2 rows in set (0.00 sec)

    mysql> select * from task_tags;
    +--------+-------+
    | taskId | tagId |
    +--------+-------+
    |     69 |     1 |
    |     70 |     1 |
    +--------+-------+
    2 rows in set (0.00 sec)


Please help me on this. Thanks all in advance.


Top
 Profile  
 
 Post subject: Re: Hibernate update child collection
PostPosted: Wed Jan 07, 2015 9:16 am 
Newbie

Joined: Mon Jan 05, 2015 10:03 am
Posts: 3
Could any one please help on this.


Top
 Profile  
 
 Post subject: Re: Hibernate update child collection
PostPosted: Sat Jan 10, 2015 6:09 am 
Newbie

Joined: Mon Jan 05, 2015 10:03 am
Posts: 3
Finally i solved this mystery like this

Code:
public Integer saveTask(TaskDTO taskDTO) {
        Integer identifier = null;
        try {
            ....
            ....
            Session session = sessionFactory.getCurrentSession();
            for(int i=0 ; i < taskDTO.getTags().size(); i++) {
                TagDTO tagDTO = taskDTO.getTags().get(i);
                TagDTO data = (TagDTO) session.createCriteria(TagDTO.class).add(Restrictions.eq("tagName", tagDTO.getTagName())).uniqueResult();
                if(null == data) {
                    taskDTO.getTags().set(i,tagDTO);
                } else {
                    taskDTO.getTags().set(i,data);
                }

            }
            session.beginTransaction(); // <-- this is key point, started transaction from here.
            for(int i = 0 ; i < taskDTO.getUsers().size() ; i++) {
                UserDTO userDTO = taskDTO.getUsers().get(i);
                userDTO = (UserDTO) session.get(UserDTO.class,userDTO.getUserId());
                userDTO.getTasks().add(taskDTO);
                taskDTO.getUsers().set(i,userDTO);
            }
            ....
            ....
            identifier = (Integer) session.save(taskDTO);
            return identifier;
        } catch (Exception ex) {
            logger.error("unable to save task ", ex);
        }
        return null;
    }

I started my transaction after getting tag from db, then i see this in my database

Code:
mysql> select * from task_tags;
+--------+-------+
| taskId | tagId |
+--------+-------+
|     11 |    11 |
|     12 |    11 |
|     13 |    11 |
|     13 |    12 |
|     13 |    13 |
+--------+-------+
5 rows in set (0.00 sec)

mysql> select * from tags;
+-------+---------+
| tagId | tagName |
+-------+---------+
|    11 | java    |
|    12 | javaEE  |
|    13 | task    |
+-------+---------+
2 rows in set (0.00 sec)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.