-->
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.  [ 5 posts ] 
Author Message
 Post subject: Collection of values are not saved
PostPosted: Wed Sep 02, 2009 1:47 pm 
Newbie

Joined: Wed Sep 02, 2009 1:35 pm
Posts: 4
Hi! I am currently following the tutorial in the documentation and trying out saving a collection of values. The problem I have is that my application is saving collection of entities(Set<Course> courses) but not the collection of values(Set<String> contactDetails).

Here is my persistent class..
Code:
public class Student {
    private Long id;
    private String firstName;
    private String lastName;
    private Set<Course> courses = new HashSet<Course>();
    private Set<String> contactDetails = new HashSet<String>();

    //getters and setters
}


Here is my mapping file
Code:
<hibernate-mapping package="com.phoenixone.school.model">
    <class name="Student" table="student">
            <id name="id" column="studentId">
                   <generator class="native" />
            </id>

            <property name="firstName" />
            <property name="lastName" />

            <set name="courses" table="student_course">
                    <key column="studentId" />
                   <many-to-many column="courseId" class="Course" />
            </set>

            <set name="contactDetails" table="contactDetails">
                    <key column="studentId" />
                    <element type="string" column="contactDetail" />
            </set>
    </class>
</hibernate-mapping>


Here is the part of my DAO where I save the persistent class...
Code:
public class StudentDaoHibernate {
    public void save(Student student){
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            session.save(student);
            session.getTransaction().commit();
    }
}


Here is my test...
Code:
public class TestStudentDaoHibernate {
    public static void main(String[] args) {
            CourseDaoHibernate courseDao = new CourseDaoHibernate();

            Course c1 = new Course();
            c1.setCourseCode("CWD");
            c1.setCourseName("Web Dev");
            courseDao.save(c1);

            Set<Course> courses = new HashSet<Course>();
            courses.add(c1);

            Student student = new Student();
            student.setFirstName("Bob");
            student.setLastName("Santos");
            student.setCourses(courses);
            student.getContactDetails().add("123456789");                 

            StudentDaoHibernate dao = new StudentDaoHibernate();
            dao.save(student);
    }
}


I've tried doing the saving of the contactDetails this way but it still does not save the String...
Code:
...
Set<String> contactDetails = new HashSet<String>();
contactDetails.add("123456789");
...
student.setContactDetails(contactDetails);
...


I am not seeing any table which contains the String "123456789" that I tried to save in my test.
I've also read from Java Persistence with Hibernate that the table which contains these values are hidden from me. Is my understanding correct?
Am I missing something or just doing the saving of collection of values wrong?

Thanks in advance!


Top
 Profile  
 
 Post subject: Re: Collection of values are not saved
PostPosted: Wed Sep 02, 2009 5:34 pm 
Newbie

Joined: Wed Sep 02, 2009 1:35 pm
Posts: 4
*bump*

Anyone knows where I went wrong?

Thanks!


Top
 Profile  
 
 Post subject: Re: Collection of values are not saved
PostPosted: Wed Sep 02, 2009 6:25 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Do you save both sides of the relationship? That might be the problem.

Where did you find that DAO? It demonstrates a few major anti-patterns with regards to transaction demarcation.

Here's a tutorial on creating simple DAOs you might want to look at:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=10introductiontodataaccessojbectsdaos

For some slightly simpler tutorials to follow, check out my signature links.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Collection of values are not saved
PostPosted: Wed Sep 02, 2009 9:18 pm 
Newbie

Joined: Wed Sep 02, 2009 1:35 pm
Posts: 4
Cameron McKenzie wrote:
Do you save both sides of the relationship? That might be the problem.

Where did you find that DAO? It demonstrates a few major anti-patterns with regards to transaction demarcation.

Here's a tutorial on creating simple DAOs you might want to look at:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=10introductiontodataaccessojbectsdaos

For some slightly simpler tutorials to follow, check out my signature links.

Hi Cameron! Thanks for the reply. I just made the DAO myself.:( I'll definitely check out the links.

For the Set<Course> I do have both sides of the relationship as Course is another persistent class. However, with Set<String> contactDetails, which is just a value type not an entity type, do I need to have both sides of the relationship and how do I do that cause there was no mention of it in the tutorial?


Top
 Profile  
 
 Post subject: Re: Collection of values are not saved
PostPosted: Thu Sep 03, 2009 3:28 pm 
Newbie

Joined: Wed Sep 02, 2009 1:35 pm
Posts: 4
*bump*

thanks in advance!


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