-->
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: How to save the child entity in unidirectional one-to-many
PostPosted: Tue Apr 04, 2017 9:08 am 
Newbie

Joined: Tue Apr 04, 2017 8:15 am
Posts: 1
Hi expert,

I am completely new in ORM world.. recently reading mapping specially one-to-many relation. but i am facing some problem to understand.

i am using this link

https://dzone.com/tutorials/java/hibern ... ons-1.html

please have a look the code.

Code:
public class Student {

  private long studentId;
private String studentName;
private Set<Phone> studentPhoneNumbers

//setter getter
}

public class Phone {

   private long phoneId;
   private String phoneType;
   private String phoneNumber;

//setter getter
}


i understand it.


Real game is here.

Code:
Set<Phone> phoneNumbers = new HashSet<Phone>();
phoneNumbers.add(new Phone("house","32354353"));
phoneNumbers.add(new Phone("mobile","9889343423"));
         
Student student = new Student("Eswar", phoneNumbers);
session.save(student);



1. why we didn't save here phoneNumbers like session.save(phoneNumbers);
2. once this above code execute then the value will be stored in both table or object(STUDENT and PHONE) right. so how will i store value in only PHONE object. or how will we implement this relation through combobox where user will select STUDENT and fill value in PHONE object not in STUDENT object. then finally save. like product/category.

3.can anyone suggest good tutorial or book where hibernate is implemented in web application. i have seen 99% tutorial explain through console or using main method.

sorry for the poor english.

thanks.


Top
 Profile  
 
 Post subject: Re: one to many mapping through Web
PostPosted: Tue Apr 04, 2017 10:15 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That tutorial is wrong since it uses a @JoinTable for a one-to-many association which is not the typical way you'd map such a relationship. Check out this one which explains which is the best way to map the @OneToMany association.

As for your questions:

1. There's no such method. Usually, you want to rely on parent-side cascading. Check out this article for more info.
2. Phone is a child, and it must always point to a parent. If you use the @ManyToOne association as explained in this article, then you can achieve your goal.


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.