• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to store composite objects in Hibernate

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Entity
@Table(name="Teacher")

public class Teacher implements Serializable {
@Id
@Column(name="id")
private int id;

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

@OneToMany
@JoinColumn(name="id")
private List<Student> student;

//getter/setters
}

/////////////////////////////

@Entity
@Table(name="Student")

public class Student implements Serializable {

@Id
@Column(name="id")
private int id;

@Column(name="stuname")
private String StudentName;

//getter/setters
}


And to store object, I am using

Teacher t1 = new Teacher();
t1.setId(10);
t1.setName("from hibernate");


Student s1 = new Student();
s1.setId(10);
s1.setStudentName("Student from hibernate");

List<Student> l = new ArrayList<Student>();
l.add(s1);

t1.setStudent(l);

getHibernateTemplate().save(t1);

The query generated by hibernate is

Hibernate: insert into Teacher (name, id) values (?, ?, ?)
Hibernate: update Student set id=? where id=?

Why am I getting update for student, this is a new object, Can any body please help me
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate doesn't know what cascade option you want the relationship to use, you need to add that to your @OneToMany Annotation, basically cascade="CascadeType.PERSIST" or something like that, I don't remember the exact class and constant off the top of my head.

Mark
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic