• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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'm still in control here. LOOK at this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic