Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Help me in understanding a code with one to many

 
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to learn Hibernate , i am completely new to this technology, while browsing i saw an example with one to many:

public class Writer
{
private int id;
private String name;
private List stories;

public void setId(int i) {
id = i;
}

public int getId() {
return id;
}

public void setName(String n) {
name = n;
}

public String getName() {
return name;
}

public void setStories(List l) {
stories = l;
}

public List getStories() {
return stories;
}
}


Story.java :

public class Story {
private int id;
private String info;
public Story(){
}
public Story(String info) {
this.info = info;
}


public void setId(int i) {
id = i;
}
public int getId() {
return id;
}
public void setInfo(String n) {
info = n;
}
public String getInfo() {
return info;
}
}


Here is the code : how to save

Writer wr = new Writer();
wr.setName("Das");
ArrayList list = new ArrayList();
list.add(new Story("Story Name 1"));
list.add(new Story("Story Name 2"));
wr.setStories(list);


Here i can only see Writer data is saved , then what about story class data??
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the code should be like this ??

Writer wr = new Writer();
wr.setName("Das");

story s1 = new story();

s1.setId(101);
s1.setInfo("rAVI");

story s2 = new story();

s2.setId(102);
s2.setInfo("Kiran");


ArrayList list = new ArrayList();
list.add(s1);
list.add(s2);
wr.setStories(list);

session.save(wr);
 
I just had the craziest dream. This tiny ad was in it.
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic