- This topic has 11 replies, 2 voices, and was last updated 19 years, 1 month ago by
Haris Peco.
-
AuthorPosts
-
SEanLon11MemberI have the following problem when trying to add to my Set “Address” from within my “Contact” class. I have tried multiple things (Setting each of the classes to lazy=”true” and multiple combos of that). I am at a loss, and can find nothing in books nor online.
Where I am calling the code from:
/** * */ package Libraries.TESTERS; import java.util.Date; import Addresses.Address; import Contacts.Contact; import Libraries.Session.SessionHelper; import Notes.Note; /** * @author Owner * */ public class Test_ContactAdditions { static SessionHelper session = new SessionHelper(); /** * @param args */ public static void main(String[] args) { testAddAddress(); //testAddNote(); } public static void testAddPhone() { } public static void testAddAddress() { Contact c = (Contact) session.getSession().get(Contact.class, 33); Address addr = new Address("1515 S. 5th", "Waco", "TX", 76706); session.save(addr); c.addAddress(addr); session.closeSession(); } public static void testAddNote() { Contact c = (Contact) session.getSession().get(Contact.class, 33); Note n = new Note("My first Note", new Date()); //session.save(n); c.addNote(n); } }
What is funny though, is that when I run my testAddPhone() it works perfectly, but anything other than that fails miserably. I will post the other code soon.
Thanks in advance for your help.
Sean
SEanLon11Memberhere is my ContactAdditions class
/** * */ package Libraries.Adders; import java.util.Date; import org.hibernate.HibernateException; import Addresses.Address; import Addresses.Contact_Address; import Addresses.Contact_AddressId; import Contacts.Contact; import Contacts.Employees.Employee; import Libraries.Session.SessionHelper; import Notes.Note; import Phones.Contact_Phone; import Phones.Contact_PhoneId; import Phones.Phone; /** * @author Owner * */ public class ContactAdditions<U> implements AdditionsIFC<U> { SessionHelper session = new SessionHelper(); /** * Assigns the given phone number to the provided user * * @see Libraries.Adders.AdditionsIFC#addPhone(Phones.Phone, U) */ public boolean addPhoneIMPL(Phone phone, U user) { // for now, we assume that the phone exists // will handle the database check within Contacts Contact c = (Contact) user; Contact_PhoneId id = new Contact_PhoneId(); id.setContacts(c); id.setPhones(phone); Contact_Phone cPhone = new Contact_Phone(id); Boolean toReturn = session.save(cPhone); // TODO Auto-generated method stub if (toReturn = true) return true; else return false; } public boolean addAddressIMPL(Address address, U user) { // for now, we assume that the address exists // will handle the database check within Contacts Contact c = (Contact) user; Contact_AddressId id = new Contact_AddressId(); id.setContacts(c); id.setAddresses(address); Contact_Address cAddress = new Contact_Address(id); cAddress.setDescription("this is the description"); Boolean toReturn = session.save(cAddress); // TODO Auto-generated method stub if (toReturn = true) return true; else return false; } public boolean addNoteIMPL(Note note, U user) { // for now, we assume that the address exists // will handle the database check within Contacts Contact c = (Contact) user; note.setContacts(c); Boolean toReturn = session.save(note); // TODO Auto-generated method stub if (toReturn = true) return true; else return false; } /** * Returns the sessionHelper variable in order to directly save from * Contacts. This has proven to be very useful, but may want to re-design * some parts so we do not have a million instances of SessionHelper out * there. */ /* * public SessionHelper getSessionHelper() { return session; } */ }
SEanLon11MemberHere is the simple calls from within my Contact class
public boolean addPhone(Phone p) { // TODO check to see if the Phone exists, if not, then create it // for now, we assume it exists Boolean toRet = additions.addPhoneIMPL(p, this); if (toRet) { this.getContactsPhoneses().add(p); return true; } else { System.out.println("Problem adding Contact Phone"); return toRet; } } public boolean addAddress(Address address) { // TODO check to see if the Phone exists, if not, then create it // for now, we assume it exists Boolean toRet = additions.addAddressIMPL(address, this); if (toRet) { this.getContactsAddresseses().add(address); return true; } else { System.out.println("Problem adding Contact Address"); return toRet; } }
As you can see, Contact extends AbstractContact, and below is the Set for each one that I am trying to manipulate. And of course, each of them has getters and setters.
private Set contactsAddresseses = new HashSet(0); private Set noteses = new HashSet(0); private Set contactsPhoneses = new HashSet(0);
Thanks again,
Sean
Haris PecoMemberSean,
Can you send database schema, mappings, POJO and error log , please
Thanks
SEanLon11MemberSchema:
-- -- Target: PostgreSQL -- Syntax: psql \i filename -- -- Date : Mar 12 2006 21:49 -- Script Generated by Database Design Studio 2.21.3 -- -- -- Create Table : 'addresses' -- addressID : -- address1 : -- address2 : -- city : -- state : -- zip : -- description : -- CREATE SEQUENCE seq_addresses_addressID INCREMENT 1 START 1; CREATE TABLE addresses ( addressID INTEGER DEFAULT NEXTVAL('seq_addresses_addressID') NOT NULL, address1 TEXT NOT NULL, address2 TEXT NULL, city TEXT NOT NULL, state TEXT NOT NULL, zip INTEGER NOT NULL, description TEXT NULL, CONSTRAINT pk_addresses PRIMARY KEY (addressID)); -- -- Create Table : 'phones' -- phoneID : -- phoneNo : -- description : -- CREATE SEQUENCE seq_phones_phoneID INCREMENT 1 START 1; CREATE TABLE phones ( phoneID INTEGER DEFAULT NEXTVAL('seq_phones_phoneID') NOT NULL, phoneNo TEXT NOT NULL, description TEXT DEFAULT NULL, CONSTRAINT pk_phones PRIMARY KEY (phoneID)); -- -- Create Table : 'contacts' -- contactID : -- gender : -- socialTitle : -- firstName : -- middleName : -- lastName : -- suffix : -- email : -- startDate : -- birthdate : -- age : figured for the user (current_date - birth_date) -- status : -- CREATE SEQUENCE seq_contacts_contactID INCREMENT 1 START 1; CREATE TABLE contacts ( contactID INTEGER DEFAULT NEXTVAL('seq_contacts_contactID') NOT NULL, gender CHAR(1) NOT NULL, socialTitle TEXT NULL, firstName TEXT NOT NULL, middleName TEXT NULL, lastName TEXT NOT NULL, suffix TEXT NULL, email TEXT NULL, startDate DATE DEFAULT current_date NOT NULL, birthdate DATE NULL, age INTEGER NULL, status CHAR(1) DEFAULT 'F' NULL, CONSTRAINT pk_contacts PRIMARY KEY (contactID)); -- -- Create Table : 'organizations' -- organizationID : -- name : -- primaryContact : -- description : -- webAddress : -- dateAdded : auto-generated -- CREATE SEQUENCE seq_organizations_organizationID INCREMENT 1 START 1; CREATE TABLE organizations ( organizationID INTEGER DEFAULT NEXTVAL('seq_organizations_organizationID') NOT NULL, name TEXT NOT NULL, primaryContact TEXT NOT NULL, description TEXT NULL, webAddress TEXT NULL, dateAdded DATE NOT NULL, CONSTRAINT pk_organizations PRIMARY KEY (organizationID)); -- -- Create Table : 'services' -- serviceID : -- name : -- description : -- CREATE SEQUENCE seq_services_serviceID INCREMENT 1 START 1; CREATE TABLE services ( serviceID INTEGER DEFAULT NEXTVAL('seq_services_serviceID') NOT NULL, name TEXT NOT NULL, description TEXT NOT NULL, CONSTRAINT pk_services PRIMARY KEY (serviceID)); -- -- Create Table : 'students' -- contactID : (references contacts.contactID) -- grade : possibly may be calculated according to the students school year -- CREATE TABLE students ( contactID INTEGER NOT NULL, grade INTEGER NOT NULL CHECK(grade <= 12 ), CONSTRAINT pk_students PRIMARY KEY (contactID), CONSTRAINT fk_students FOREIGN KEY (contactID) REFERENCES contacts (contactID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'employees' -- contactID : (references contacts.contactID) -- employeeID : -- endDate : auto-generated at termination -- title : -- jobDescription : -- CREATE SEQUENCE seq_employees_employeeID INCREMENT 1 START 1; CREATE TABLE employees ( contactID INTEGER NOT NULL, employeeID INTEGER DEFAULT NEXTVAL('seq_employees_employeeID') NOT NULL, endDate DATE DEFAULT NULL, title TEXT NOT NULL, jobDescription TEXT NOT NULL, CONSTRAINT pk_employees PRIMARY KEY (contactID,employeeID), CONSTRAINT fk_employees FOREIGN KEY (contactID) REFERENCES contacts (contactID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'contacts_addresses' -- addressID : (references addresses.addressID) -- contactID : (references contacts.contactID) -- description : -- CREATE TABLE contacts_addresses ( addressID INTEGER NOT NULL, contactID INTEGER NOT NULL, description TEXT NULL, CONSTRAINT pk_contacts_addresses PRIMARY KEY (addressID,contactID), CONSTRAINT fk_contacts_addresses FOREIGN KEY (addressID) REFERENCES addresses (addressID) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_contacts_addresses2 FOREIGN KEY (contactID) REFERENCES contacts (contactID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'contacts_phones' -- phoneID : (references phones.phoneID) -- contactID : (references contacts.contactID) -- CREATE TABLE contacts_phones ( phoneID INTEGER NOT NULL, contactID INTEGER NOT NULL, CONSTRAINT pk_contacts_phones PRIMARY KEY (phoneID,contactID), CONSTRAINT fk_contacts_phones FOREIGN KEY (phoneID) REFERENCES phones (phoneID) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_contacts_phones2 FOREIGN KEY (contactID) REFERENCES contacts (contactID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'instructors' -- employeeID : (references employees.employeeID) -- contactID : (references employees.contactID) -- gradePreference : their preferred grade of choice -- CREATE TABLE instructors ( employeeID INTEGER NOT NULL, contactID INTEGER NOT NULL, gradePreference INTEGER NOT NULL, CONSTRAINT pk_instructors PRIMARY KEY (employeeID,contactID), CONSTRAINT fk_instructors FOREIGN KEY (contactID,employeeID) REFERENCES employees (contactID,employeeID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'fullTimers' -- employeeID : (references employees.employeeID) -- contactID : (references employees.contactID) -- duties : -- comments : -- CREATE TABLE fullTimers ( employeeID INTEGER NOT NULL, contactID INTEGER NOT NULL, duties TEXT NULL, comments TEXT NULL, CONSTRAINT pk_fullTimers PRIMARY KEY (employeeID,contactID), CONSTRAINT fk_fullTimers FOREIGN KEY (contactID,employeeID) REFERENCES employees (contactID,employeeID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'otherEmployees' -- employeeID : (references employees.employeeID) -- contactID : (references employees.contactID) -- description : -- CREATE TABLE otherEmployees ( employeeID INTEGER NOT NULL, contactID INTEGER NOT NULL, description TEXT DEFAULT NULL, CONSTRAINT pk_otherEmployees PRIMARY KEY (employeeID,contactID), CONSTRAINT fk_otherEmployees FOREIGN KEY (contactID,employeeID) REFERENCES employees (contactID,employeeID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'contacts_organizations' -- contactID : (references contacts.contactID) -- organizationID : (references organizations.organizationID) -- CREATE TABLE contacts_organizations ( contactID INTEGER NOT NULL, organizationID INTEGER NOT NULL, CONSTRAINT pk_contacts_organizations PRIMARY KEY (contactID,organizationID), CONSTRAINT fk_contacts_organizations FOREIGN KEY (contactID) REFERENCES contacts (contactID) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_contacts_organizations2 FOREIGN KEY (organizationID) REFERENCES organizations (organizationID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'notes' -- noteID : -- enteredBy : this is the person who entered the note, which should be automatically set -- note : -- contactID : (references contacts.contactID) -- noteDate : this should be auto-generated -- noteTime : this should be auto-generated and not seen by the user -- CREATE SEQUENCE seq_notes_noteID INCREMENT 1 START 1; CREATE TABLE notes ( noteID INTEGER DEFAULT NEXTVAL('seq_notes_noteID') NOT NULL, enteredBy TEXT NULL, note TEXT NOT NULL, contactID INTEGER NULL, noteDate DATE DEFAULT current_date NOT NULL, noteTime TIME NULL, CONSTRAINT pk_notes PRIMARY KEY (noteID), CONSTRAINT fk_notes FOREIGN KEY (contactID) REFERENCES contacts (contactID) MATCH FULL ON DELETE SET NULL ON UPDATE CASCADE); -- -- Create Table : 'students_services' -- contactID : (references students.contactID) -- serviceID : (references services.serviceID) -- semester : FA, SP, S1, S2 -- yearTaken : easier to be an Integer then a DATE -- CREATE TABLE students_services ( contactID INTEGER NOT NULL, serviceID INTEGER NOT NULL, semester TEXT NOT NULL, yearTaken INTEGER NOT NULL, CONSTRAINT pk_students_services PRIMARY KEY (contactID,serviceID), CONSTRAINT fk_students_services FOREIGN KEY (contactID) REFERENCES students (contactID) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_students_services2 FOREIGN KEY (serviceID) REFERENCES services (serviceID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'employee_services' -- contactID : (references employees.contactID) -- employeeID : (references employees.employeeID) -- serviceID : (references services.serviceID) -- semester : -- yearTaught : auto-generated -- CREATE TABLE employee_services ( contactID INTEGER NOT NULL, employeeID INTEGER NOT NULL, serviceID INTEGER NOT NULL, semester CHAR(2) NOT NULL, yearTaught DATE NOT NULL, CONSTRAINT pk_employee_services PRIMARY KEY (contactID,employeeID,serviceID), CONSTRAINT fk_employee_services FOREIGN KEY (contactID,employeeID) REFERENCES employees (contactID,employeeID) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_employee_services2 FOREIGN KEY (serviceID) REFERENCES services (serviceID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'clients' -- contactID : (references contacts.contactID) -- inquery_Date : -- inquery_Time : -- relationship : parent, grandparent, donator, etc. -- CREATE TABLE clients ( contactID INTEGER NOT NULL, inquery_Date DATE NOT NULL, inquery_Time TIME NOT NULL, relationship TEXT NULL, CONSTRAINT pk_clients PRIMARY KEY (contactID), CONSTRAINT fk_clients FOREIGN KEY (contactID) REFERENCES contacts (contactID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'churches' -- organizationID : (references organizations.organizationID) -- pastor : -- relgion : -- CREATE TABLE churches ( organizationID INTEGER NOT NULL, pastor TEXT NOT NULL, relgion TEXT NOT NULL, CONSTRAINT pk_churches PRIMARY KEY (organizationID), CONSTRAINT fk_churches FOREIGN KEY (organizationID) REFERENCES organizations (organizationID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'schools' -- organizationID : (references organizations.organizationID) -- principle : -- schoolDistrict : -- CREATE TABLE schools ( organizationID INTEGER NOT NULL, principle TEXT NULL, schoolDistrict TEXT NULL, CONSTRAINT pk_schools PRIMARY KEY (organizationID), CONSTRAINT fk_schools FOREIGN KEY (organizationID) REFERENCES organizations (organizationID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'businesses' -- organizationID : (references organizations.organizationID) -- businessType : -- CREATE TABLE businesses ( organizationID INTEGER NOT NULL, businessType TEXT NOT NULL, CONSTRAINT pk_businesses PRIMARY KEY (organizationID), CONSTRAINT fk_businesses FOREIGN KEY (organizationID) REFERENCES organizations (organizationID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'otherOrganizations' -- organizationID : (references organizations.organizationID) -- type : -- description : -- CREATE TABLE otherOrganizations ( organizationID INTEGER NOT NULL, type TEXT NOT NULL, description TEXT NOT NULL, CONSTRAINT pk_otherOrganizations PRIMARY KEY (organizationID), CONSTRAINT fk_otherOrganizations FOREIGN KEY (organizationID) REFERENCES organizations (organizationID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'organizations_addresses' -- organizationID : (references organizations.organizationID) -- addressID : (references addresses.addressID) -- description : -- CREATE TABLE organizations_addresses ( organizationID INTEGER NOT NULL, addressID INTEGER NOT NULL, description TEXT NULL, CONSTRAINT pk_organizations_addresses PRIMARY KEY (organizationID,addressID), CONSTRAINT fk_organizations_addresses FOREIGN KEY (organizationID) REFERENCES organizations (organizationID) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_organizations_addresses2 FOREIGN KEY (addressID) REFERENCES addresses (addressID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'contacts_contacts' -- contactID : (references contacts.contactID) -- contactID1 : (references contacts.contactID) -- relationship : I need a good way to represent the relationships -- CREATE TABLE contacts_contacts ( contactID INTEGER NOT NULL, contactID1 INTEGER NOT NULL, relationship TEXT NOT NULL, CONSTRAINT pk_contacts_contacts PRIMARY KEY (contactID,contactID1), CONSTRAINT fk_contacts_contacts FOREIGN KEY (contactID) REFERENCES contacts (contactID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE, CONSTRAINT fk_contacts_contacts2 FOREIGN KEY (contactID1) REFERENCES contacts (contactID) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE); -- -- Create Table : 'schoolTeachers' -- contactID : (references contacts.contactID) -- CREATE TABLE schoolTeachers ( contactID INTEGER NOT NULL, CONSTRAINT pk_schoolTeachers PRIMARY KEY (contactID), CONSTRAINT fk_schoolTeachers FOREIGN KEY (contactID) REFERENCES contacts (contactID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Create Table : 'organizations_phones' -- organizationID : (references organizations.organizationID) -- phoneID : (references phones.phoneID) -- CREATE TABLE organizations_phones ( organizationID INTEGER NOT NULL, phoneID INTEGER NOT NULL, CONSTRAINT pk_organizations_phones PRIMARY KEY (organizationID,phoneID), CONSTRAINT fk_organizations_phones FOREIGN KEY (organizationID) REFERENCES organizations (organizationID) MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_organizations_phones2 FOREIGN KEY (phoneID) REFERENCES phones (phoneID) MATCH FULL ON DELETE NO ACTION ON UPDATE CASCADE); -- -- Permissions for: 'public' -- GRANT ALL ON addresses TO GROUP public; GRANT ALL ON phones TO GROUP public; GRANT ALL ON contacts TO GROUP public; GRANT ALL ON organizations TO GROUP public; GRANT ALL ON services TO GROUP public; GRANT ALL ON students TO GROUP public; GRANT ALL ON employees TO GROUP public; GRANT ALL ON contacts_addresses TO GROUP public; GRANT ALL ON contacts_phones TO GROUP public; GRANT ALL ON instructors TO GROUP public; GRANT ALL ON fullTimers TO GROUP public; GRANT ALL ON otherEmployees TO GROUP public; GRANT ALL ON contacts_organizations TO GROUP public; GRANT ALL ON notes TO GROUP public; GRANT ALL ON students_services TO GROUP public; GRANT ALL ON employee_services TO GROUP public; GRANT ALL ON clients TO GROUP public; GRANT ALL ON churches TO GROUP public; GRANT ALL ON schools TO GROUP public; GRANT ALL ON businesses TO GROUP public; GRANT ALL ON otherOrganizations TO GROUP public; GRANT ALL ON organizations_addresses TO GROUP public; GRANT ALL ON contacts_contacts TO GROUP public; GRANT ALL ON schoolTeachers TO GROUP public; GRANT ALL ON organizations_phones TO GROUP public;
SEanLon11MemberRelevant Mappings for Contact:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="Contacts.Contact" table="contacts" schema="public"> <id name="contactid" type="integer"> <column name="contactid" /> <generator class="increment"></generator> </id> <property name="gender" type="string"> <column name="gender" length="1" not-null="true" /> </property> <property name="socialtitle" type="string"> <column name="socialtitle" /> </property> <property name="firstname" type="string"> <column name="firstname" not-null="true" /> </property> <property name="middlename" type="string"> <column name="middlename" /> </property> <property name="lastname" type="string"> <column name="lastname" not-null="true" /> </property> <property name="suffix" type="string"> <column name="suffix" /> </property> <property name="email" type="string"> <column name="email" /> </property> <property name="startdate" type="date"> <column name="startdate" length="4" not-null="true" /> </property> <property name="birthdate" type="date"> <column name="birthdate" length="4" /> </property> <property name="age" type="integer"> <column name="age" /> </property> <property name="status" type="string"> <column name="status" length="1" /> </property> <set name="contactsContactsesForContactid1" inverse="true"> <key> <column name="contactid1" not-null="true" /> </key> <one-to-many class="Contacts.Contact_Contact" /> </set> <set name="contactsOrganizationses" inverse="true"> <key> <column name="contactid" not-null="true" /> </key> <one-to-many class="Organizations.Contact_Organization" /> </set> <set name="schoolteacherses" inverse="true"> <key> <column name="contactid" not-null="true" unique="true" /> </key> <one-to-many class="Contacts.SchoolTeacher" /> </set> <set name="contactsAddresseses" inverse="true"> <key> <column name="contactid" not-null="true" /> </key> <one-to-many class="Addresses.Contact_Address" /> </set> <set name="noteses" inverse="true"> <key> <column name="contactid" /> </key> <one-to-many class="Notes.Note" /> </set> <set name="contactsPhoneses" inverse="true"> <key> <column name="contactid" not-null="true" /> </key> <one-to-many class="Phones.Contact_Phone" /> </set> <set name="studentses" inverse="true"> <key> <column name="contactid" not-null="true" unique="true" /> </key> <one-to-many class="Contacts.Student" /> </set> <set name="employeeses" inverse="true"> <key> <column name="contactid" not-null="true" /> </key> <one-to-many class="Contacts.Employees.Employee" /> </set> <set name="contactsContactsesForContactid" inverse="true"> <key> <column name="contactid" not-null="true" /> </key> <one-to-many class="Contacts.Contact_Contact" /> </set> <set name="clientses" inverse="true"> <key> <column name="contactid" not-null="true" unique="true" /> </key> <one-to-many class="Contacts.Client" /> </set> </class> </hibernate-mapping>
SEanLon11MemberAddress mapping:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="Addresses.Address" table="addresses" schema="public"> <id name="addressid" type="integer"> <column name="addressid" /> <generator class="increment"></generator> </id> <property name="address1" type="string"> <column name="address1" not-null="true" /> </property> <property name="address2" type="string"> <column name="address2" /> </property> <property name="city" type="string"> <column name="city" not-null="true" /> </property> <property name="state" type="string"> <column name="state" not-null="true" /> </property> <property name="zip" type="integer"> <column name="zip" not-null="true" /> </property> <property name="description" type="string"> <column name="description" /> </property> <set name="organizationsAddresseses" inverse="true"> <key> <column name="addressid" not-null="true" /> </key> <one-to-many class="Addresses.Organization_Address" /> </set> <set name="contactsAddresseses" inverse="true"> <key> <column name="addressid" not-null="true" /> </key> <one-to-many class="Addresses.Contact_Address" /> </set> </class> </hibernate-mapping>
SEanLon11Member<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="Addresses.Contact_Address" table="contacts_addresses" schema="public"> <composite-id name="id" class="Addresses.Contact_AddressId"> <key-many-to-one name="addresses" class="Addresses.Address"> <column name="addressid" /> </key-many-to-one> <key-many-to-one name="contacts" class="Contacts.Contact"> <column name="contactid" /> </key-many-to-one> </composite-id> <property name="description" type="string"> <column name="description" /> </property> </class> </hibernate-mapping>
SEanLon11MemberAbstractContact.java
package Contacts; import java.util.Date; import java.util.HashSet; import java.util.Set; import Libraries.Setters.SetContactSets; /** * AbstractContact generated by MyEclipse - Hibernate Tools */ public abstract class AbstractContact implements java.io.Serializable { // Fields private Integer contactid; private String gender; private String socialtitle; private String firstname; private String middlename; private String lastname; private String suffix; private String email; private Date startdate; private Date birthdate; private Integer age; private String status; private Set contactsContactsesForContactid1 = new HashSet(0); private Set contactsOrganizationses = new HashSet(0); private Set schoolteacherses = new HashSet(0); private Set contactsAddresseses = new HashSet(0); private Set noteses = new HashSet(0); private Set contactsPhoneses = new HashSet(0); private Set studentses = new HashSet(0); private Set employeeses = new HashSet(0); private Set contactsContactsesForContactid = new HashSet(0); private Set clientses = new HashSet(0); // Constructors /** default constructor */ public AbstractContact() { } /** minimal constructor */ public AbstractContact(String gender, String firstname, String lastname, Date startdate) { this.gender = gender; this.firstname = firstname; this.lastname = lastname; this.startdate = startdate; } /** full constructor */ public AbstractContact(String gender, String socialtitle, String firstname, String middlename, String lastname, String suffix, String email, Date startdate, Date birthdate, Integer age, String status, Set contactsContactsesForContactid1, Set contactsOrganizationses, Set schoolteacherses, Set contactsAddresseses, Set noteses, Set contactsPhoneses, Set studentses, Set employeeses, Set contactsContactsesForContactid, Set clientses) { this.gender = gender; this.socialtitle = socialtitle; this.firstname = firstname; this.middlename = middlename; this.lastname = lastname; this.suffix = suffix; this.email = email; this.startdate = startdate; this.birthdate = birthdate; this.age = age; this.status = status; this.contactsContactsesForContactid1 = contactsContactsesForContactid1; this.contactsOrganizationses = contactsOrganizationses; this.schoolteacherses = schoolteacherses; this.contactsAddresseses = contactsAddresseses; this.noteses = noteses; this.contactsPhoneses = contactsPhoneses; this.studentses = studentses; this.employeeses = employeeses; this.contactsContactsesForContactid = contactsContactsesForContactid; this.clientses = clientses; } // Property accessors public Integer getContactid() { return this.contactid; } public void setContactid(Integer contactid) { this.contactid = contactid; } public String getGender() { return this.gender; } public void setGender(String gender) { this.gender = gender; } public String getSocialtitle() { return this.socialtitle; } public void setSocialtitle(String socialtitle) { this.socialtitle = socialtitle; } public String getFirstname() { return this.firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getMiddlename() { return this.middlename; } public void setMiddlename(String middlename) { this.middlename = middlename; } public String getLastname() { return this.lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getSuffix() { return this.suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public Date getStartdate() { return this.startdate; } public void setStartdate(Date startdate) { this.startdate = startdate; } public Date getBirthdate() { return this.birthdate; } public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } public String getStatus() { return this.status; } public void setStatus(String status) { this.status = status; } public Set getContactsContactsesForContactid1() { return this.contactsContactsesForContactid1; } public void setContactsContactsesForContactid1( Set contactsContactsesForContactid1) { this.contactsContactsesForContactid1 = contactsContactsesForContactid1; } public Set getContactsOrganizationses() { return this.contactsOrganizationses; } public void setContactsOrganizationses(Set contactsOrganizationses) { this.contactsOrganizationses = contactsOrganizationses; } public Set getSchoolteacherses() { return this.schoolteacherses; } public void setSchoolteacherses(Set schoolteacherses) { this.schoolteacherses = schoolteacherses; } public Set getContactsAddresseses() { return this.contactsAddresseses; } public void setContactsAddresseses(Set contactsAddresseses) { this.contactsAddresseses = contactsAddresseses; } public Set getNoteses() { return this.noteses; } public void setNoteses(Set noteses) { this.noteses = noteses; } public Set getContactsPhoneses() { return this.contactsPhoneses; } public void setContactsPhoneses(Set contactsPhoneses) { this.contactsPhoneses = contactsPhoneses; } public Set getStudentses() { return this.studentses; } public void setStudentses(Set studentses) { this.studentses = studentses; } public Set getEmployeeses() { return this.employeeses; } public void setEmployeeses(Set employeeses) { this.employeeses = employeeses; } public Set getContactsContactsesForContactid() { return this.contactsContactsesForContactid; } public void setContactsContactsesForContactid( Set contactsContactsesForContactid) { this.contactsContactsesForContactid = contactsContactsesForContactid; } public Set getClientses() { return this.clientses; } public void setClientses(Set clientses) { this.clientses = clientses; } }
SEanLon11MemberAbstractAddress.java
package Addresses; import java.util.HashSet; import java.util.Set; /** * AbstractAddress generated by MyEclipse - Hibernate Tools */ public abstract class AbstractAddress implements java.io.Serializable { // Fields private Integer addressid; private String address1; private String address2; private String city; private String state; private Integer zip; private String description; private Set organizationsAddresseses = new HashSet(0); private Set contactsAddresseses = new HashSet(0); // Constructors /** default constructor */ public AbstractAddress() { } /** minimal constructor */ public AbstractAddress(String address1, String city, String state, Integer zip) { this.address1 = address1; this.city = city; this.state = state; this.zip = zip; } /** full constructor */ public AbstractAddress(String address1, String address2, String city, String state, Integer zip, String description, Set organizationsAddresseses, Set contactsAddresseses) { this.address1 = address1; this.address2 = address2; this.city = city; this.state = state; this.zip = zip; this.description = description; this.organizationsAddresseses = organizationsAddresseses; this.contactsAddresseses = contactsAddresseses; } // Property accessors public Integer getAddressid() { return this.addressid; } public void setAddressid(Integer addressid) { this.addressid = addressid; } public String getAddress1() { return this.address1; } public void setAddress1(String address1) { this.address1 = address1; } public String getAddress2() { return this.address2; } public void setAddress2(String address2) { this.address2 = address2; } public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } public String getState() { return this.state; } public void setState(String state) { this.state = state; } public Integer getZip() { return this.zip; } public void setZip(Integer zip) { this.zip = zip; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public Set getOrganizationsAddresseses() { return this.organizationsAddresseses; } public void setOrganizationsAddresseses(Set organizationsAddresseses) { this.organizationsAddresseses = organizationsAddresseses; } public Set getContactsAddresseses() { return this.contactsAddresseses; } public void setContactsAddresseses(Set contactsAddresseses) { this.contactsAddresseses = contactsAddresseses; } }
SEanLon11MemberIf you need anything else, please let me know.
Thanks,
Sean
Haris PecoMemberSean,
I make your schema and call MyEclipse Reverse engineering and i get next (examples for Contacts, Addresses
and ContactAddresses)mappings :
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="test.Contacts" table="contacts" schema="public"> <id name="contactid" type="integer"> <column name="contactid" /> <generator class="sequence" > <param name="sequence">seq_contacts_contactID</param> </generator> </id> <property name="gender" type="string"> <column name="gender" length="1" not-null="true" /> </property> <property name="socialtitle" type="string"> <column name="socialtitle" /> </property> <property name="firstname" type="string"> <column name="firstname" not-null="true" /> </property> <property name="middlename" type="string"> <column name="middlename" /> </property> <property name="lastname" type="string"> <column name="lastname" not-null="true" /> </property> <property name="suffix" type="string"> <column name="suffix" /> </property> <property name="email" type="string"> <column name="email" /> </property> <property name="startdate" type="date"> <column name="startdate" length="13" not-null="true" /> </property> <property name="birthdate" type="date"> <column name="birthdate" length="13" /> </property> <property name="age" type="integer"> <column name="age" /> </property> <property name="status" type="string"> <column name="status" length="1" /> </property> <set name="contactsContactsesForContactid1" inverse="true"> <key> <column name="contactid1" not-null="true" /> </key> <one-to-many class="test.ContactsContacts" /> </set> <set name="contactsOrganizationses" inverse="true"> <key> <column name="contactid" not-null="true" /> </key> <one-to-many class="test.ContactsOrganizations" /> </set> <set name="schoolteacherses" inverse="true"> <key> <column name="contactid" not-null="true" unique="true" /> </key> <one-to-many class="test.Schoolteachers" /> </set> <set name="contactsAddresseses" inverse="true"> <key> <column name="contactid" not-null="true" /> </key> <one-to-many class="test.ContactsAddresses" /> </set> <set name="noteses" inverse="true"> <key> <column name="contactid" /> </key> <one-to-many class="test.Notes" /> </set> <set name="contactsPhoneses" inverse="true"> <key> <column name="contactid" not-null="true" /> </key> <one-to-many class="test.ContactsPhones" /> </set> <set name="studentses" inverse="true"> <key> <column name="contactid" not-null="true" unique="true" /> </key> <one-to-many class="test.Students" /> </set> <set name="employeeses" inverse="true"> <key> <column name="contactid" not-null="true" /> </key> <one-to-many class="test.Employees" /> </set> <set name="contactsContactsesForContactid" inverse="true"> <key> <column name="contactid" not-null="true" /> </key> <one-to-many class="test.ContactsContacts" /> </set> <set name="clientses" inverse="true"> <key> <column name="contactid" not-null="true" unique="true" /> </key> <one-to-many class="test.Clients" /> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="test.Addresses" table="addresses" schema="public"> <id name="addressid" type="integer"> <column name="addressid" /> <generator class="sequence" > <param name="sequence">seq_addresses_addressID</param> </generator> </id> <property name="address1" type="string"> <column name="address1" not-null="true" /> </property> <property name="address2" type="string"> <column name="address2" /> </property> <property name="city" type="string"> <column name="city" not-null="true" /> </property> <property name="state" type="string"> <column name="state" not-null="true" /> </property> <property name="zip" type="integer"> <column name="zip" not-null="true" /> </property> <property name="description" type="string"> <column name="description" /> </property> <set name="organizationsAddresseses" inverse="true"> <key> <column name="addressid" not-null="true" /> </key> <one-to-many class="test.OrganizationsAddresses" /> </set> <set name="contactsAddresseses" inverse="true"> <key> <column name="addressid" not-null="true" /> </key> <one-to-many class="test.ContactsAddresses" /> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse - Hibernate Tools --> <hibernate-mapping> <class name="test.ContactsAddresses" table="contacts_addresses" schema="public"> <composite-id name="id" class="test.ContactsAddressesId"> <key-many-to-one name="addresses" class="test.Addresses"> <column name="addressid" /> </key-many-to-one> <key-many-to-one name="contacts" class="test.Contacts"> <column name="contactid" /> </key-many-to-one> </composite-id> <property name="description" type="string"> <column name="description" /> </property> </class> </hibernate-mapping>
POJOs
/** * AbstractContacts generated by MyEclipse - Hibernate Tools */ public abstract class AbstractContacts implements java.io.Serializable { // Fields private Integer contactid; private String gender; private String socialtitle; private String firstname; private String middlename; private String lastname; private String suffix; private String email; private Date startdate; private Date birthdate; private Integer age; private String status; private Set contactsContactsesForContactid1 = new HashSet(0); private Set contactsOrganizationses = new HashSet(0); private Set schoolteacherses = new HashSet(0); private Set contactsAddresseses = new HashSet(0); private Set noteses = new HashSet(0); private Set contactsPhoneses = new HashSet(0); private Set studentses = new HashSet(0); private Set employeeses = new HashSet(0); private Set contactsContactsesForContactid = new HashSet(0); private Set clientses = new HashSet(0); // Constructors /** default constructor */ public AbstractContacts() { } /** minimal constructor */ public AbstractContacts(String gender, String firstname, String lastname, Date startdate) { this.gender = gender; this.firstname = firstname; this.lastname = lastname; this.startdate = startdate; } /** full constructor */ public AbstractContacts(String gender, String socialtitle, String firstname, String middlename, String lastname, String suffix, String email, Date startdate, Date birthdate, Integer age, String status, Set contactsContactsesForContactid1, Set contactsOrganizationses, Set schoolteacherses, Set contactsAddresseses, Set noteses, Set contactsPhoneses, Set studentses, Set employeeses, Set contactsContactsesForContactid, Set clientses) { this.gender = gender; this.socialtitle = socialtitle; this.firstname = firstname; this.middlename = middlename; this.lastname = lastname; this.suffix = suffix; this.email = email; this.startdate = startdate; this.birthdate = birthdate; this.age = age; this.status = status; this.contactsContactsesForContactid1 = contactsContactsesForContactid1; this.contactsOrganizationses = contactsOrganizationses; this.schoolteacherses = schoolteacherses; this.contactsAddresseses = contactsAddresseses; this.noteses = noteses; this.contactsPhoneses = contactsPhoneses; this.studentses = studentses; this.employeeses = employeeses; this.contactsContactsesForContactid = contactsContactsesForContactid; this.clientses = clientses; } // Property accessors public Integer getContactid() { return this.contactid; } public void setContactid(Integer contactid) { this.contactid = contactid; } public String getGender() { return this.gender; } public void setGender(String gender) { this.gender = gender; } public String getSocialtitle() { return this.socialtitle; } public void setSocialtitle(String socialtitle) { this.socialtitle = socialtitle; } public String getFirstname() { return this.firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getMiddlename() { return this.middlename; } public void setMiddlename(String middlename) { this.middlename = middlename; } public String getLastname() { return this.lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public String getSuffix() { return this.suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public Date getStartdate() { return this.startdate; } public void setStartdate(Date startdate) { this.startdate = startdate; } public Date getBirthdate() { return this.birthdate; } public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } public String getStatus() { return this.status; } public void setStatus(String status) { this.status = status; } public Set getContactsContactsesForContactid1() { return this.contactsContactsesForContactid1; } public void setContactsContactsesForContactid1(Set contactsContactsesForContactid1) { this.contactsContactsesForContactid1 = contactsContactsesForContactid1; } public Set getContactsOrganizationses() { return this.contactsOrganizationses; } public void setContactsOrganizationses(Set contactsOrganizationses) { this.contactsOrganizationses = contactsOrganizationses; } public Set getSchoolteacherses() { return this.schoolteacherses; } public void setSchoolteacherses(Set schoolteacherses) { this.schoolteacherses = schoolteacherses; } public Set getContactsAddresseses() { return this.contactsAddresseses; } public void setContactsAddresseses(Set contactsAddresseses) { this.contactsAddresseses = contactsAddresseses; } public Set getNoteses() { return this.noteses; } public void setNoteses(Set noteses) { this.noteses = noteses; } public Set getContactsPhoneses() { return this.contactsPhoneses; } public void setContactsPhoneses(Set contactsPhoneses) { this.contactsPhoneses = contactsPhoneses; } public Set getStudentses() { return this.studentses; } public void setStudentses(Set studentses) { this.studentses = studentses; } public Set getEmployeeses() { return this.employeeses; } public void setEmployeeses(Set employeeses) { this.employeeses = employeeses; } public Set getContactsContactsesForContactid() { return this.contactsContactsesForContactid; } public void setContactsContactsesForContactid(Set contactsContactsesForContactid) { this.contactsContactsesForContactid = contactsContactsesForContactid; } public Set getClientses() { return this.clientses; } public void setClientses(Set clientses) { this.clientses = clientses; } } import java.util.HashSet; import java.util.Set; /** * AbstractAddresses generated by MyEclipse - Hibernate Tools */ public abstract class AbstractAddresses implements java.io.Serializable { // Fields private Integer addressid; private String address1; private String address2; private String city; private String state; private Integer zip; private String description; private Set organizationsAddresseses = new HashSet(0); private Set contactsAddresseses = new HashSet(0); // Constructors /** default constructor */ public AbstractAddresses() { } /** minimal constructor */ public AbstractAddresses(String address1, String city, String state, Integer zip) { this.address1 = address1; this.city = city; this.state = state; this.zip = zip; } /** full constructor */ public AbstractAddresses(String address1, String address2, String city, String state, Integer zip, String description, Set organizationsAddresseses, Set contactsAddresseses) { this.address1 = address1; this.address2 = address2; this.city = city; this.state = state; this.zip = zip; this.description = description; this.organizationsAddresseses = organizationsAddresseses; this.contactsAddresseses = contactsAddresseses; } // Property accessors public Integer getAddressid() { return this.addressid; } public void setAddressid(Integer addressid) { this.addressid = addressid; } public String getAddress1() { return this.address1; } public void setAddress1(String address1) { this.address1 = address1; } public String getAddress2() { return this.address2; } public void setAddress2(String address2) { this.address2 = address2; } public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } public String getState() { return this.state; } public void setState(String state) { this.state = state; } public Integer getZip() { return this.zip; } public void setZip(Integer zip) { this.zip = zip; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public Set getOrganizationsAddresseses() { return this.organizationsAddresseses; } public void setOrganizationsAddresseses(Set organizationsAddresseses) { this.organizationsAddresseses = organizationsAddresseses; } public Set getContactsAddresseses() { return this.contactsAddresseses; } public void setContactsAddresseses(Set contactsAddresseses) { this.contactsAddresseses = contactsAddresseses; } } /** * AbstractContactsAddresses generated by MyEclipse - Hibernate Tools */ public abstract class AbstractContactsAddresses implements java.io.Serializable { // Fields private ContactsAddressesId id; private String description; // Constructors /** default constructor */ public AbstractContactsAddresses() { } /** full constructor */ public AbstractContactsAddresses(String description) { this.description = description; } // Property accessors public ContactsAddressesId getId() { return this.id; } public void setId(ContactsAddressesId id) { this.id = id; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } }
I make test with
Contacts with id 4
Addresses with id 1 and 2
ContactsAdresses with ids (1,4)I want add address id 2 to contacts.This is code :
public void testAddAddress() { Session session = getSession(); Contacts c = (Contacts) session.get(Contacts.class, new Integer(4)); Set contactsAddresses = c.getContactsAddresseses(); assertTrue(contactsAddresses.size() == 1); Addresses addr = (Addresses) session.get(Addresses.class,new Integer(2)); // we have to add linked table or use many-to-many ContactsAddresses ca = new ContactsAddresses(); ContactsAddressesId cid = new ContactsAddressesId(); cid.setAddresses(addr); cid.setContacts(c); ca.setDescription("desc"); ca.setId(cid); Transaction tx = getSession().beginTransaction(); session.save(ca); c.getContactsAddresseses().add(ca); session.merge(c); tx.commit(); c = (Contacts) session.get(Contacts.class, new Integer(4)); contactsAddresses = c.getContactsAddresseses(); assertTrue(contactsAddresses.size() == 2); }
Best
-
AuthorPosts