views:

223

answers:

4

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"&gt;
<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.

+1  A: 

No. The problem is that Hibernate does not recognize Municipality as an entity. Is it configured properly (e.g. in your persistence.xml)?

Edit: The error message is weird for this situation, and I don't know if that is really the problem, but Address.municipality should be defined as @ManyToOne (as there will be multiple addresses referencing the same municipality).

Henning
I think yes, im edit my post adding the xml config.
blow
@blow: That looks fine. What happens when you query "from Municipality"?
Henning
"from Municipality" works good, retrieve a list of all my Municipality.The problem may be that i have an @entity(Municipality) associate @OneToOne to an @emebddable(Address) embedded into an @entity(Person) and not directly to Person?
blow
@blow: I've updated my answer.
Henning
Thank you, i tried to change @OneToOne in @ManyToOne but problem rest.Maybe i have to add some association in Municipality too?I tried this in Municipality:@OneToManyprivate List<Address> addresList;The problem stay...
blow
+2  A: 

I suppose your error is just because you have defined a @Embeddable class (Address) as Entity. @Embeddable class is not a plain Entity, so remove it from your hibernate.cfg.xml file

<mapping class="Entities.Person"/>
<mapping class="Entities.Municipality"/>
Arthur Ronald F D Garcia
Ah! that sounds reasonable.
Henning
+1 - I was writing exactly the same when I saw this post. Don't know that it fixes this, but it's an error.
Don Roby
Yes is reasonable, but the problem stay.
blow
@blow It seems right! Are you sure There is no missing piece of code ???
Arthur Ronald F D Garcia
The code is the same i have posted, exept i have setter and getter obviusly.
blow
+1  A: 

Are you damn sure you use @javax.persistence.Entity instead of @org.hibernate.annotations.Entity?

OK, another guess. Could you try changing the @OneToOne to @OneToOne(targetEntity = Municipality.class)?

Petar Minchev
Sure, my imports are this:import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Embedded;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;
blow
OK, updated my answer.
Petar Minchev
+3  A: 

The problem is... i lunch my querys into netbeans "HQL Query()" tab, i try to create a query

session.createQuery("from Person");

and all works good.

Sorry guys my fault, i do this test first... i thought that HQL wizard into netbeans was a reliable tool to test queries...

In this case, wich of answer i have to choice? All are reasonable and full of tips!

Sorry again.

blow
@blow Thank you for your reply. At least, you have provided what happened (+1)
Arthur Ronald F D Garcia