Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6119843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:35:59+00:00 2026-05-23T15:35:59+00:00

i got 2 entities, Student and Phone, and a relationship one-to-many between them. @Entity

  • 0

i got 2 entities, Student and Phone, and a relationship one-to-many between them.

@Entity 
@Table(name = "STUDENT") 
public class Student { 

    private long studentId; 
    private String studentName; 
    private Set<Phone> studentPhoneNumbers = new HashSet<Phone>(0); 

    public Student() { 
    } 

    public Student(String studentName, Set<Phone> studentPhoneNumbers) { 
        this.studentName = studentName; 
        this.studentPhoneNumbers = studentPhoneNumbers; 
    } 

    @Id 
    @GeneratedValue 
    @Column(name = "STUDENT_ID") 
    public long getStudentId() { 
        return this.studentId; 
    } 

    public void setStudentId(long studentId) { 
        this.studentId = studentId; 
    } 

    @Column(name = "STUDENT_NAME", nullable = false, length = 100) 
    public String getStudentName() { 
        return this.studentName; 
    } 

    public void setStudentName(String studentName) { 
        this.studentName = studentName; 
    } 

    @OneToMany (mappedBy="student", cascade = CascadeType.ALL)       

    public Set<Phone> getStudentPhoneNumbers() { 
        return this.studentPhoneNumbers; 
    } 

    public void setStudentPhoneNumbers(Set<Phone> studentPhoneNumbers) { 
        this.studentPhoneNumbers = studentPhoneNumbers; 
    } 

} 


@Entity 
@Table(name = "PHONE") 
public class Phone { 

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

    private Student student; 
    private Set<Student> students = new HashSet<Student>(0); 

    public Phone() { 
    } 

    public Phone(String phoneType, String phoneNumber) { 
        this.phoneType = phoneType; 
        this.phoneNumber = phoneNumber; 
    } 

    @Id 
    @GeneratedValue 
    @Column(name = "PHONE_ID") 
    public long getPhoneId() { 
        return this.phoneId; 
    } 

    public void setPhoneId(long phoneId) { 
        this.phoneId = phoneId; 
    } 

    @Column(name = "PHONE_TYPE", nullable = false, length=10) 
    public String getPhoneType() { 
        return this.phoneType; 
    } 

    public void setPhoneType(String phoneType) { 
        this.phoneType = phoneType; 
    } 

    @Column(name = "PHONE_NUMBER", nullable = false, length=15) 
    public String getPhoneNumber() { 
        return this.phoneNumber; 
    } 

    public void setPhoneNumber(String phoneNumber) { 
        this.phoneNumber = phoneNumber; 
    } 

    @ManyToOne(cascade = CascadeType.ALL) 
    @JoinColumn (name="STUDENT_ID") 
    public Student getStudent() { 
        return this.student; 
    } 

    public void setStudent(Student student) { 
        this.student = student; 
    }   

here is the code of applicationContext.xml:

  ... 
 <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>   
        <property name="url" value="jdbc:mysql://localhost/Project"/>   
        <property name="username" value="root"/>   
        <property name="password" value="root"/>   
    </bean>   

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">   
        <property name="dataSource" ref="myDataSource" />   
        <property name="annotatedClasses">   
            <list>   
                <value>com.domain.Student</value>                         
                <value>com.domain.Phone</value>                           
            </list>   
        </property>   
        <property name="hibernateProperties">   
            <props>   
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>   
                <prop key="hibernate.show_sql">true</prop>   
                <prop key="hibernate.hbm2ddl.auto">create</prop>   
            </props>   
        </property>   
    </bean>   

 <bean id="myClassDAO" class="com.project.dao.ClassDAOImpl"> 
        <property name="sessionFactory" ref="mySessionFactory"/> 
 </bean>   

...  

And my ClassDAOImpl looks like:

    public class ClassDAOImpl{ 

    private HibernateTemplate hibernateTemplate; 
    private Session session; 
    public void setSessionFactory(SessionFactory sessionFactory) { 
            this.hibernateTemplate = new HibernateTemplate(sessionFactory); 
            this.session = sessionFactory.openSession(); 
    }     

    public void updateStudent(){ 

            Transaction transaction = session.beginTransaction();           
            Student s = (Student)session.get(Student.class, new Long(1));   
            Set<Phone> phoneNumbers =s.getStudentPhoneNumbers();   
            phoneNumbers.add(new Phone ("house","12342342"));   
            s.setStudentPhoneNumbers(phoneNumbers);   
            session.update(s);   
            transaction.commit(); 

    } 

}  

I just want to add a phone to the student with primary key “1”. For this purpose, first I get the student and then I update the phone set by adding a new one. Finally I update the entity Student.
I looked at my Phone table, and there was a new row with the new phone, but the foreign key (STUDENT_ID) appeared with “Null” value, therefore the relationship is not registered. How could i fix it?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:36:00+00:00Added an answer on May 23, 2026 at 3:36 pm

    Try this:

    public void updateStudent(){ 
    
            Transaction transaction = session.beginTransaction();           
            Student s = (Student)session.get(Student.class, new Long(1));   
            Set<Phone> phoneNumbers =s.getStudentPhoneNumbers();   
            Phone phone = new Phone("house", "12342342");
            phone.setStudent(s);
            phoneNumbers.add(phone);   
            s.setStudentPhoneNumbers(phoneNumbers);   
            session.update(s);   
            transaction.commit(); 
    
        }
    

    Or better yet, add a utility method to student like so:

    public void addPhone(Phone phone) {
        if (phone == null) throw new IllegalArgumentException("Cannot add a null phone to a Student!");
        phone.setStudent(this);
        getStudents().add(phone);
    }
    

    And change your updateStudent method to be:

    public void updateStudent(){ 
    
            Transaction transaction = session.beginTransaction();           
            Student s = (Student)session.get(Student.class, new Long(1));   
            Phone phone = new Phone("house", "12342342");
            s.addPhone(phone);
            session.update(s);   
            transaction.commit(); 
    
        } 
    

    Also make sure you have set your cascade options to cascade to phone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got two entities with a one-to-many relationship between them. The entity that holds
I got these 2 entities: @javax.persistence.Entity public class Book { @javax.persistence.EmbeddedId private BookPK id;
I've got two entities ServiceDownload.java @Entity public class ServiceDownload implements Serializable { private static
Symfony 2, Doctrine 2.1. I've got 3 entities, one of them intermediate (join table).
i've got two Entities with combinded primarky keys: @Entity @IdClass(APK.class) public class A {
for simplicity, here is my simplified model: I got 2 entities: @entity public class
i've got following example xml: <entity id=1> <name>computer</name> <type>category</type> <entities> <entity id=2> <name>mac</name> <type>category</type>
So I got entities Level and Tile. Level has a to-many relationship with Tile.
I've got two entities in the entity framework. Now I want to seperate them
I'm working in Hibernate. I got 3 entities, that's the relationship among them: User

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.