Added by on 2013-07-07

The goal is to implement the one to many relationship:

States
id name
States
id name state

States.java

public class States implements Serializable{
	(...)
	private id;
	private String name;
	private List city;
	(...)
}

Cities.java

public class Cities implements Serializable{
	(...)
	private id;
	private String name;
	(...)
}

INSERT.java

public static void main(){
	(...)

	States st = new States();
	st.getId(1);
	st.setName("Texas");

	Cities c1 = new Cities();
	c1.setName("Laredo");

	Cities c2 = new Cities();
	c2.setName("Dallas");	

	Cities c3 = new Cities();
	c3.setName("San-Antonio");

	List list_c = new ArrayList();
	list_c.add(c1);
	list_c.add(c2);
	list_c.add(c3);
	st.setCity(list_c);

	session.save(st);
	session.save(c1);
	session.save(c2);
	session.save(c3);

	(...)
}

SELECT.java

	public static void main(){
		(...)

		States st = (States) session.get(States.class, new String("Texas"));
		List city = st.getCity();
		for(Cities c:city){
			Sysytem.out.println( c.getName() );
		}

		(...)
	}

Category:

Hibernate

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*