Hi Everyone, I am facing some problem with my Hibernate Many-one application I am able to execute the application partially.The problem is tht "I am not able to retrieve the values from the mapping class" Here i am providing the whole code wht i did till now
People.java ------------ package relation;
public class People { String firstName; String lastName;
String PeopleID;
public People () { }
public String getLastName() { return lastName; }
public String getFirstName() { return firstName; }
public void setFirstName( String name) {firstName = name; }
public void setLastName( String name) { lastName = name; }
public String getPeopleID() { return PeopleID; }
public void setPeopleID(String p_sid) {PeopleID = p_sid; }
}
Address.java ------------ package relation;
public class Address { String Street; String City; String AddressId; String PeopleID;
public String getCity() { return City; }
public void setCity(String city) { this. City = city; } public String getAddressId() { return AddressId; } public void setAddressId(String id) { this. AddressId = id; } public String getStreet() { return Street; } public void setStreet(String street) { this. Street = street; } public void setPeopleID(String id) { this. PeopleID = id; } public String getPeopleID() { return PeopleID; }
}
HibernateUtil(For creating the session factory) ---------------------------------------------- package util;
private static final SessionFactory sessionFactory;
static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); System.out.println("Session factory ---->"+ sessionFactory); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
public static SessionFactory getSessionFactory() { return sessionFactory; }
In the above application i am able to create and insert values in to person table comming to the address i am able to create address table but i can't insert values in to the address table(Where mapping operation is taking place in address table)
1. Take advantage of smart defaults. For instance your Id attribute and column. You should have an attribute in your class called just "id", so Hibernate will automatically append the class name to the id, like if it was in a Person object, then Hibernate will map the "id" to the "person_id" column in the database table without you having to map it yourself. Meaning you don't have to tell Hibernate what column to use for the <id> tag.
2. Have your attributes be the type they should be. All of your attributes in your classes are all Strings. Id is definitely not a String.
3. You are mapping only one side of your relationship. So you will only be able to traverse in one direction, and create/save/update/delete in that direction and not reverse. This is not necessarily wrong, as in some cases it should be one directional, and not bi-directional. But if it only is one direction, know which direction that is and don't expect it to work the other direction.