Hibernate (Unknown entity: java.util.HashSet)
以下是会用到的文件,比较多,麻烦大家了。主要是我这里面有四个表,有一个没有关系的我就没写出来,我现在contact表没办法生成,报错是 Unknown entity: java.util.HashSet ,不知道是哪里错了…… 以及配置文件配没配错……
麻烦了。
contact.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="domain">
<class name="Contact" table="Contact_Table">
<id name="idcontact" type="long" column="ID_CONTACT" >
<generator class="increment"/>
</id>
<property name="firstName" column="FirstName" ></property>
<property name="lastName" column="LastName" ></property>
<property name="email" column="Email" ></property>
<many-to-one name="address" column="ID_ADD" unique="true"></many-to-one>
<set name="phonenumber" inverse="true" >
<key column="ID_CONTACT"></key>
<one-to-many class="PhoneNumber"/>
</set>
<set name="contactgroups" inverse="true" table="jointable">
<key column="ID_CONTACT"></key>
<many-to-many class="ContactGroup" column="ID_ContactGroup"></many-to-many>
</set>
</class>
</hibernate-mapping>
contactgroup.hbm.xm;
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="domain">
<class name="ContactGroup" table="ContactGroup_Table">
<id name="idgroup" type="long" column="ID_GROUP" >
<generator class="increment"/>
</id>
<property name="groupName" type="string"></property>
<set name="contacts" table="jointable">
<key column="ID_ContactGroup"></key>
<many-to-many class="Contact" column="ID_Contact"></many-to-many>
</set>
</class>
</hibernate-mapping>
phonenumber.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="domain">
<class name="PhoneNumber" table="PhoneNumber_Table">
<id name="idphone">
<generator class="increment"/>
</id>
<property name="phoneKind" type="string"></property>
<property name="phoneNumber" type="string"></property>
<many-to-one name="contact" class ="Contact" column="ID_CONTACT"></many-to-one>
</class>
</hibernate-mapping>
DAOcontact.java
package domain;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import util.HibernateUtil;
public class DAOContact {
String Street,City,Zip,Country,phoneKind1, phoneKind2, phoneKind3, phoneNumber1, phoneNumber2, phoneNumber3,group,group1;
public DAOContact(String Street,String City,String Zip,String Country,String phoneKind1, String phoneKind2, String phoneKind3, String phoneNumber1, String phoneNumber2, String phoneNumber3,String group,String group1){
this.Street = Street;
this.City = City;
this.Zip = Zip;
this.Country = Country;
this.phoneKind1 = phoneKind1;
this.phoneKind2 = phoneKind2;
this.phoneKind3 = phoneKind3;
this.phoneNumber1 = phoneNumber1;
this.phoneNumber2 = phoneNumber2;
this.phoneNumber3 = phoneNumber3;
this.group=group;
this.group1=group1;
}
public void addContact(long id, String firstName, String lastName, String emailC ){
Session session = null;
try{
session = HibernateUtil.getSessionFactory().openSession();
org.hibernate.Transaction tx = session.beginTransaction();
Contact contact = new Contact();
Address add=new Address(Street, City, Zip, Country);
PhoneNumber pho1=new PhoneNumber(phoneKind1,phoneNumber1);
PhoneNumber pho2=new PhoneNumber(phoneKind2,phoneNumber2);
PhoneNumber pho3=new PhoneNumber(phoneKind3,phoneNumber3);
ContactGroup gro=new ContactGroup(group);
ContactGroup gro1=new ContactGroup(group1);
Set phone =new HashSet();
Set groups = new HashSet();
Set contacts = new HashSet();
contact.setIdcontact(id);
contact.setFirstName(firstName);
contact.setLastName(lastName);
contact.setEmail(emailC);
contact.setAddress(add);
phone.add(pho1);
phone.add(pho2);
phone.add(pho3);
groups.add(gro);
groups.add(gro1);
contact.setPhonenumber(phone);
contact.setContactgroups(groups);
session.save(phone);
session.save(add);
session.save(contact);
tx.commit();
System.out.println("Done");
}catch (Exception e){
System.out.println(e.getMessage());
}finally{
session.close();
}
}
}
contact.java
package domain;
import java.util.HashSet;
import java.util.Set;
public class Contact {
long idcontact;
String firstName;
String lastName;
String email;
Set contactgroups = new HashSet();
Address address;
Set phonenumber =new HashSet();
public Contact() {
// TODO Auto-generated constructor stub
}
public Contact(long idContact, String firstName, String lastName,
String email) {
super();
this.idcontact = idContact;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public long getIdcontact() {
return idcontact;
}
public void setIdcontact(long id_contact) {
this.idcontact = id_contact;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Set getContactgroups() {
return contactgroups;
}
public Set getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(Set phonenumber) {
this.phonenumber = phonenumber;
}
public void setContactgroups(Set contactgroups) {
this.contactgroups = contactgroups;
}
}
contactgroup.java
package domain;
import java.util.HashSet;
import java.util.Set;
public class ContactGroup {
long idgroup;
String groupName;
Set contacts = new HashSet();
public ContactGroup(){
}
public ContactGroup(String groupName) {
super();
this.groupName = groupName;
}
public long getIdgroup() {
return idgroup;
}
public void setIdgroup(long id_group) {
this.idgroup = id_group;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public Set getContacts() {
return contacts;
}
public void setContacts(Set contacts) {
this.contacts = contacts;
}
}
phonenumber.java
package domain;
public class PhoneNumber {
long idphone;
String phoneKind;
String phoneNumber;
Contact contact;
public PhoneNumber(){
}
public PhoneNumber(String phoneKind, String phoneNumber) {
super();
this.phoneKind = phoneKind;
this.phoneNumber = phoneNumber;
}
public long getIdphone() {
return idphone;
}
public void setIdphone(long id_phone) {
this.idphone = id_phone;
}
public String getPhoneKind() {
return phoneKind;
}
public void setPhoneKind(String phoneKind1) {
this.phoneKind = phoneKind1;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber1) {
this.phoneNumber = phoneNumber1;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
}
以上是会用到的文件,比较多,麻烦大家了。主要是我这里面有四个表,有一个没有关系的我就没写出来,我现在contact表没办法生成,报错是 Unknown entity: java.util.HashSet ,不知道是哪里错了…… 以及配置文件配没配错……
麻烦了。