views:

570

answers:

3

hello good people i'm developping an application where all the pojos are exposed as interface but we map the real implementation class.we are using spring and JPA annotation.i'm about to test the one-to-one relationship and i'm having a light problem with the interface.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionContainer' defined in class path resource [META-INF/model-config.xml]:
Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in class path resource [META-INF/model-config.xml]:
Invocation of init method failed; nested exception is org.hibernate.AnnotationException:
@OneToOne or @ManyToOne on com.mycompany.project.subproject.model.UserAccountImpl.profile references an unknown entity: com.mycompany.project.

so before this class all the other mapped class are working as expected so i'll only post part of the applicationContext file that i named model-config.xml

<property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
        </props>
    </property>
    <property name="annotatedClasses">
        <list>
           ...
            <value>com.mycompany.project.subproject.model.UserProfileImpl</value>
            <value>com.mycompany.project.subproject.model.UserAccountImpl</value>
           ...
        </list>
    </property>

here are the two involved class UserProfileImpl.java and UserAccountImpl.java

//UserAccountImpl Class
@Entity
@Table(name ="USER_ACCOUNT")
public class UserAccountImpl implements UserAccount {

  @Id @GeneratedValue
  @Column(name="USER_ACCOUNT_ID")
  private Long ID;

  ...

  @OneToOne
  @JoinColumn(name="USER_PROFILE_ID")
  private UserProfile profile;

  ...
 }

//UserProfileImpl class
@Entity
@Table(name="USER_PROFILE")
public class UserProfileImpl implements UserProfile {

 @Id @GeneratedValue
 @Column(name="USER_PROFILE_ID")
 private Long ID;
 ....

 @OneToOne(mappedBy="profile")
 private UserAccount userAccount;
 ....

}

i'm still not very confortable with hibernate yet so i'm wondering if i should Change the UserProfile reference in UserAccountImpl to UserProfileImpl.Then again the same can happen in the UserProfileImpl for userAccount reference since it's a bidirectional navigation stuff. What's the best option that will no break the consistency of the structure? Thanks for reading this

+1  A: 

You have these options:

  1. You must tell Hibernate somehow which class to use for the interface UserAccount. Currently, the most simple solution is to use a concrete type instead of the interface in your UserProfileImpl.

  2. You can use @Target to specify the implementation to use (see [the docs][1]).

  3. You can map the field with a custom UserType. This allows to chose the mapping (which implementation to use for an interface) at runtime but you must write the code to copy the fields between your business objects and the DB yourself (no automatic mapping anymore).

Aaron Digulla
thanks for the insight. will get back to you.thanks a lot
black sensei
this one works for me thanks again
black sensei
+1  A: 

You could try the following:

@OneToOne(mappedBy="profile", targetEntity=UserAccountImpl.class)
 private UserAccount userAccount
david
thanks bruv this is the most easiest one.will try it
black sensei
A: 

Does UserProfile have to be a separate Entity? You could model this as a Component, and combine the UserAccount and UserProfile tables into one. Your object model would still have a separate UserProfile object, it would just be a value object owned by UserAccount.

Not every object has to be implemented as an Entity,and One-to-One mappings are quite rare in practice ....

alasdairg
mmmh sounds very suitable here.But i'm not really ok (i mean i'll have to read) with component. So i'll look at it in the next milestone maybe.But definitely you solution is cool for me.thanks
black sensei