William Cézar William Cézar - 5 months ago 27
Java Question

Hibernat exception mapping an entity

I had this project and I have to include another enitity with an @OneToMany dependency,the classe name is empresa (enterprise), but when the code is running I get this exception.


br.com.caelum.vraptor.InterceptionException: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.assist.model.entity.Colaborador.Empresa in com.assist.model.entity.Empresa.colaboradores
br.com.caelum.vraptor.interceptor.StepInvoker.invokeMethod(StepInvoker.java:69)
br.com.caelum.vraptor.interceptor.StepInvoker.tryToInvoke(StepInvoker.java:55)
br.com.caelum.vraptor.interceptor.StepInvoker$Proxy$_$$WeldClientProxy.tryToInvoke(Unknown Source)
br.com.caelum.vraptor.interceptor.InterceptorExecutor.executeAround(InterceptorExecutor.java:75)
br.com.caelum.vraptor.interceptor.InterceptorExecutor$Proxy$
$$WeldClientProxy.executeAround(Unknown Source)
br.com.caelum.vraptor.interceptor.AspectStyleInterceptorHandler.execute(AspectStyleInterceptorHandler.java:85)
br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83)
br.com.caelum.vraptor.core.DefaultInterceptorStack.start(DefaultInterceptorStack.java:93)
br.com.caelum.vraptor.core.DefaultInterceptorStack$Proxy$
$$_WeldClientProxy.start(Unknown Source)
br.com.caelum.vraptor.observer.RequestHandlerObserver.handle(RequestHandlerObserver.java:93)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)
org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266)
org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253)
org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232)
org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169)
org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165)
org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119)
org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112)
org.jboss.weld.event.EventImpl.fire(EventImpl.java:83)
br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:123)


I dont know how to fix,and what is causing this, I'm pretty new in java and hibernate, here is my classes.

entity Colaborador =


@SkipSerialization
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Id_Empresa", nullable = false, foreignKey = @ForeignKey(name = "FK_COLABORADOR_EMPRESA"))
public Empresa empresa;



Entity Empresa


@OneToMany(mappedBy = "empresa", targetEntity = Colaborador.class, cascade = CascadeType.ALL, orphanRemoval = true)
private Set colaboradores;


One empresa could have many colaboradores, if someone could help I appreciate , thanks.

Answer

First of all, create the class and put mapping class in hibernate configuration. Ex:

<hibernate-configuration>

    <session-factory>
        <property name="hibernate.connection.url">${conn-string}</property>
        <property name="hibernate.connection.username">${db-user}</property>
        <property name="hibernate.connection.password">${db-pass}</property>
        <property name="hibernate.dialect">com.ftu.utils.FtuCustomOracleDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.min_size">1</property>
        <property name="hibernate.c3p0.max_size">50</property>
        <property name="hibernate.c3p0.timeout">30000</property>
        <property name="hibernate.c3p0.checkoutTimeout">30000</property>
        <property name="hibernate.c3p0.max_statements">1</property>
        <property name="hibernate.c3p0.idle_test_period">100</property>

        <property name="show_sql">false</property>
        <property name="format_sql">true</property>

        <mapping class="com.assist.model.entity.Empresa " />
    </session-factory>
</hibernate-configuration>

Then you do the relashionship @oneToMany.