Hi all, im new in hibernate and JPA and i have some problems with annotations.
My target is to create this table in db (PERSON_TABLE with personal-details)
ID ADDRESS NAME SURNAME MUNICIPALITY_ID
First of all, i have a MUNICIPALITY table in db containing all municipality of my country. I mapped this table in this ENTITY:
@Entity
public class Municipality implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String country;
private String province;
private String name;
@Column(name="cod_catasto")
private String codCatastale;
private String cap;
public Municipality() {
}
...
Then i make an EMBEDDABLE class Address containing fields that realize a simple address...
@Embeddable
public class Address implements Serializable {
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_municipality")
private Municipality municipality;
@Column(length=45)
private String address;
public Address() {
}
...
Finally i embedded this class into Person ENTITY
@Entity
public class Person implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private String surname;
@Embedded
private Address address;
public Person() {
}
...
All works good when i have to save a new Person record, in fact hibernate creates a PERSON_TABLE as i want, but if i try to retrieve a Person record i have an exception. HQL is simply "from Person" The excpetion is (Entities is the package containing all classes above-mentioned):
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on Entities.Person.address.municipality references an unknown entity: Entities.Municipality
Is the @OneToOne annotation the problem?
My hibernate.cfg.xml is this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:tcp://localhost/DB_PATH</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="Entities.Person"/>
<mapping class="Entities.Municipality"/>
<mapping class="Entities.Address"/>
</session-factory>
</hibernate-configuration>
Thanks.