Hibernate异常映射实体

时间:2016-06-16 18:39:08

标签: java hibernate

我有这个项目,我必须包含另一个具有@OneToMany依赖项的实体,类名是empresa(企业),但是当代码运行时我得到了这个异常。

  

br.com.caelum.vraptor.InterceptionException:org.hibernate.AnnotationException:mappedBy引用一个未知的目标实体属性:com.assist.model.entity.Empresa.colaboradores中的com.assist.model.entity.Colaborador.Empresa       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(原生方法)       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)

我不知道如何修复,导致这种情况的原因,我在java和hibernate中都很新,这是我的课程。

实体Colaborador =

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

实体Empresa

  

@OneToMany(mappedBy =" empresa",targetEntity = Colaborador.class,cascade = CascadeType.ALL,orphanRemoval = true)       私人套装colaboradores;

一个empresa可能有很多colaboradores。

1 个答案:

答案 0 :(得分:1)

首先,创建类并将映射类放在hibernate配置中。例如:

<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>

然后你做关系@oneToMany。