This week's book giveaway is in the Cloud forum.
We're giving away four copies of The Business Blockchain and have William Mougayar on-line!
See this thread for details.
Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me in understanding a code with one to many

 
RaviNada Kiran
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);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic