一对一双向映射不适用于Spring和Hibernate

时间:2019-03-20 12:04:47

标签: spring hibernate spring-mvc spring-annotations hibernate-annotations

我有两个表employee和address,仅在雇员实体中使用OnetoOne映射时,它从地址表中获取对应的地址。但是,当我尝试在地址实体中使用OnetoOne来获取corespondent员工时,会引发以下错误

  

org.springframework.beans.factory.BeanCreationException:创建名称为'adminController'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private rentd.service.UserServices rentd.controller.AdminController.userService;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'UserServices'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有rent.dao.UserDao rentd.serviceimpl.UserServiceImpl.userDao;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'UserDao'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private org.hibernate.SessionFactory rent.daoimpl.UserDaoImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建在ServletContext资源[/WEB-INF/applicationContext.xml]中定义的名称为'sessionFactory'的bean时出错:调用init方法失败;嵌套的异常是org.hibernate.AnnotationException:未知,映射于:rentd.model.Employee_Address.employee,引用的属性未知:rentd.model.Employee.AddDetail   由以下原因引起:org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private rentd.service.UserServices rent.controller.AdminController.userService;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'UserServices'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有rent.dao.UserDao rentd.serviceimpl.UserServiceImpl.userDao;嵌套的异常是 org.springframework.beans.factory.BeanCreationException :创建名称为'UserDao'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private org.hibernate.SessionFactory rent.daoimpl.UserDaoImpl.sessionFactory;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在ServletContext资源[/WEB-INF/applicationContext.xml]中定义的名称为'sessionFactory'的bean时出错:调用init方法失败;嵌套的异常是org.hibernate.AnnotationException:未知,映射于:rentd.model.Employee_Address.employee,引用的属性未知:rentd.model.Employee.AddDetail   由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名称为'UserServices'的bean时出错:自动连接依赖项的注入失败;嵌套的异常为 org.springframework.beans.factory.BeanCreationException :无法自动连线字段:私有的rentd.dao.UserDao rentd.serviceimpl.UserServiceImpl.userDao;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'UserDao'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private org.hibernate.SessionFactory rent.daoimpl.UserDaoImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建在ServletContext资源[/WEB-INF/applicationContext.xml]中定义的名称为'sessionFactory'的bean时出错:调用init方法失败;嵌套的异常是org.hibernate.AnnotationException:未知,映射于:rentd.model.Employee_Address.employee,引用的属性未知:rentd.model.Employee.AddDetail   由以下原因导致:org.springframework.beans.factory.BeanCreationException:无法自动连线字段:私有的rent.dao.UserDao rentd.serviceimpl.UserServiceImpl.userDao;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'UserDao'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private org.hibernate.SessionFactory rent.daoimpl.UserDaoImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建在ServletContext资源[/WEB-INF/applicationContext.xml]中定义的名称为'sessionFactory'的bean时出错:调用init方法失败;嵌套的异常是org.hibernate.AnnotationException:未知,映射于:rentd.model.Employee_Address.employee,引用的属性未知:rentd.model.Employee.AddDetail   由以下原因导致:org.springframework.beans.factory.BeanCreationException:创建名称为'UserDao'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private org.hibernate.SessionFactory rent.daoimpl.UserDaoImpl.sessionFactory;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建在ServletContext资源[/WEB-INF/applicationContext.xml]中定义的名称为'sessionFactory'的bean时出错:调用init方法失败;嵌套的异常是org.hibernate.AnnotationException:未知,映射于:rentd.model.Employee_Address.employee,引用的属性未知:rentd.model.Employee.AddDetail   原因:org.springframework.beans.factory.BeanCreationException:无法自动连线字段:私有org.hibernate.SessionFactory rent.daoimpl.UserDaoImpl.sessionFactory;嵌套的异常是 org.springframework.beans.factory.BeanCreationException: 在ServletContext资源中定义名称为“ sessionFactory”的bean创建错误 [/WEB-INF/applicationContext.xml]:调用init方法失败;嵌套的异常是org.hibernate.AnnotationException:未知,映射于:rentd.model.Employee_Address.employee,引用的属性未知:rentd.model.Employee.AddDetail   原因:org.hibernate.AnnotationException: 未知映射依据:rentd.model.Employee_Address.employee,引用的属性未知:rentd.model.Employee.AddDetail

@Entity
@Table(name = "Tbl_Employee")
public class Employee {

private int EmpId;
private String EmpName;
private Employee_Address AddDetail;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="EmployeeId", updatable = false, nullable = false)
public int getEmpId() {
    return EmpId;
}
public void setEmpId(int empId) {
    EmpId = empId;
}

@Column(name="EmployeeName")
public String getEmpName() {
    return EmpName;
}
public void setEmpName(String empName) {
    EmpName = empName;
}

@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="EmpAdd_FK")
public Employee_Address getAddDetail() {
    return AddDetail;
}
public void setAddDetail(Employee_Address addDetail) {
    AddDetail = addDetail;
}



}
  

Employee_Address实体类

@Entity
@Table(name = "Tbl_Employee_Address")
public class Employee_Address {

private int AddId;
private String Address;
private Employee employee;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="AddressId", updatable = false, nullable = false)
public int getAddId() {
    return AddId;
}
public void setAddId(int addId) {
    AddId = addId;
}

@Column(name="AddressDetail")
public String getAddress() {
    return Address;
}
public void setAddress(String address) {
    Address = address;
}

@OneToOne(mappedBy="AddDetail",cascade=CascadeType.ALL, fetch = FetchType.EAGER)
public Employee getEmployee() {
    return employee;
}
public void setEmployee(Employee employee) {
    this.employee = employee;
}


}
  

applicationContext.xml

    <beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- <context:property-placeholder location="classpath:resources/database.properties" 
    /> -->
<context:annotation-config />
<context:component-scan base-package="rentd" />

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
    </mvc:message-converters>
</mvc:annotation-driven>

<mvc:resources mapping="/resources/**" location="/resources/" />

<mvc:resources mapping="/images/**" location="file:///C:/RentdUpload/Images/" />
<mvc:resources mapping="/profileimages/**" location="file:///C:/RentdUpload/Images/ProfilePic/" />
<mvc:resources mapping="/categoryimages/**" location="file:///C:/RentdUpload/Images/CategoryPic/" />

<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    id="dataSource">

    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>

    <property name="url" value="jdbc:sqlserver://CRUZZE-05:1433;database=RENTD[Dev-001];integratedSecurity=true" />

</bean>

<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
    id="sessionFactory">
    <property name="dataSource" ref="dataSource"></property>
    <property name="annotatedClasses">
        <list>
            <value>rentd.model.Brand</value>
            <value>rentd.model.Category</value>
            <value>rentd.model.City</value>
            <value>rentd.model.Connection</value>
            <value>rentd.model.Country</value>
            <value>rentd.model.Courier</value>
            <value>rentd.model.Customer</value>
            <value>rentd.model.EmailSettings</value>
            <value>rentd.model.Notifications</value>
            <value>rentd.model.OTPVerification</value>
            <value>rentd.model.ProductMaster</value>
            <value>rentd.model.State</value>
            <value>rentd.model.SubCategory</value>
            <value>rentd.model.Vendor</value>
            <value>rentd.model.WishList</value>
            <value>rentd.model.Currency</value>
            <value>rentd.model.BillingAddress</value>
            <value>rentd.model.ShippingAddress</value>
            <value>rentd.model.Order</value>
            <value>rentd.model.OrderDetail</value>
            <value>rentd.model.UservendorMapping</value> 
            <value>rentd.model.Vendor</value>
            <value>rentd.model.TaxSetup</value>
            <value>rentd.model.InvoicenumberSeries</value> 
            <value>rentd.model.RentalStatus</value> 
            <value>rentd.model.ProductRating</value>
            <value>rentd.model.Department</value> 
            <value>rentd.model.Designation</value> 
            <value>rentd.model.RentdUser</value> 
            <value>rentd.model.Profile</value> 
            <value>rentd.model.SmsSettings</value>
            <value>rentd.model.ErrModel</value>
            <value>rentd.model.LoginStatus</value>
            <value>rentd.model.Feedback</value>
            <value>rentd.model.Forms</value>
            <value>rentd.model.FormDetail</value>
            <value>rentd.model.ProductChild</value>
            <value>rentd.model.IPv4</value>
            <value>rentd.model.ApkUpdate</value>
            <value>rentd.model.Payment</value>
            <value>rentd.model.Membership</value>
            <value>rentd.model.Employee</value>
            <value>rentd.model.Employee_Address</value>
        </list>
    </property>


    <!-- When I comment hibernate properties the hibernate query is not getting 
        printed on the console. but DB connectivity is functioning -->
    <property name="hibernateProperties">
        <props>
            <prop key="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</prop>

            <prop key="connection.url">jdbc:sqlserver://CRUZZE-05:1433;database=RENTD[Dev-001];integratedSecurity=true</prop>

            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>

            <prop key="hibernate.hbm2ddl.auto">update</prop>

            <prop key="hibernate.show_sql">true</prop>

            <prop key="format_sql">true</prop>

            <prop key="hibernate.search.default.directory_provider"> org.hibernate.search.store.impl.FSDirectoryProvider</prop>

            <prop key="hibernate.search.default.indexBase"> C:\RentdIn-local\RentdInApp\target\lucene\indexes</prop>

            <prop key="hibernate.search.rebuildIndex">false</prop>

            <prop key="hibernate.search.indexing_strategy">manual</prop>

        </props>
    </property>
</bean>

<bean class="org.springframework.orm.hibernate4.HibernateTransactionManager"
    id="hibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
  

pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RentdIn</groupId>
<artifactId>RentdIn</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>RentdIn Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
    <org.springframework-version>4.2.4.RELEASE</org.springframework-version>
    <spring-security-version>4.0.3.RELEASE</spring-security-version>
    <spring-data-jpa-version>1.8.2.RELEASE</spring-data-jpa-version>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring-data-jpa-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> 
        <version>${org.springframework-version}</version> </dependency> -->

    <!-- <dependency> -->
    <!-- <groupId>org.springframework</groupId> -->
    <!-- <artifactId>spring-orm</artifactId> -->
    <!-- <version>${org.springframework-version}</version> -->
    <!-- </dependency> -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <!-- Hibernate framework -->

    <!-- Hibernate library dependecy start -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <!-- <version>4.3.8.Final</version> -->
        <version>4.2.11.Final</version>
    </dependency>

    <!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> 
        <version>4.2.4.Release</version> </dependency> -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.8.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>

    <dependency>
        <groupId>antlr</groupId>
        <artifactId>antlr</artifactId>
        <version>2.7.7</version>
    </dependency>


    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20170516</version>
    </dependency>

    <!-- <dependency> <groupId>com.restfb</groupId> <artifactId>restfb</artifactId> 
        <version>1.18.1</version> </dependency> -->


    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>net.minidev</groupId>
        <artifactId>json-smart</artifactId>
        <version>1.0.9</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.1</version>
    </dependency>

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4</version>
    </dependency>

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.6</version>
    </dependency>

    <dependency>
        <groupId>com.googlecode.linkedin-j</groupId>
        <artifactId>linkedin-j-core</artifactId>
        <version>1.0.416</version>
        <!-- <type>pom</type> -->
    </dependency>
    <dependency>
        <groupId>com.restfb</groupId>
        <artifactId>restfb</artifactId>
        <version>1.18.1</version>
    </dependency>
    <dependency>
        <groupId>oauth.signpost</groupId>
        <artifactId>signpost-core</artifactId>
        <version>1.2.1.1</version>
    </dependency>
    <dependency>
        <groupId>com.knowably</groupId>
        <artifactId>twitter4j-core</artifactId>
        <version>4.0.5</version>
    </dependency>



    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz-jobs</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.1.5</version>
    </dependency>
     <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.0</version>
    <scope>runtime</scope>
     </dependency>
     <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.0</version>
    <scope>runtime</scope>
        </dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.0</version>
    <scope>runtime</scope>
     </dependency>
        <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.14</version>
     </dependency>
     <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
        </dependency>

 <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
    </dependency> 

 <!-- HIBERNATE SEARCH -  START  -->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-search-engine -->
<dependency>
 <groupId>org.hibernate</groupId>
  <artifactId>hibernate-search-engine</artifactId>
 <version>4.1.1.Final</version>
 <!--  <version>4.3.0.Final</version> -->
</dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-search-orm -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>4.1.1.Final</version>
</dependency>

 <!-- START - Lucene Dependencies -->
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>3.5.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>3.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queries -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queries</artifactId>
<version>3.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers</artifactId>
<version>3.5.0</version>
</dependency>    
    <!-- END - Lucene Dependencies -->



 <!-- HIBERNATE SEARCH -  END  -->
</dependencies>

<build>
    <finalName>RentdIn</finalName>
      <!-- DIRECTORY CREATION USING MAVEN PLUGIN -  START  -->
        <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <argLine>-Xmx1024m</argLine>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <id>mkdirs</id>
                    <phase>validate</phase>
                    <configuration>
                        <target>                            
                        <!-- <mkdir dir="data/lucene/indexes" /> -->
                             <mkdir dir="target/lucene/indexes" /> 
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings 
                only. It has no influence on the Maven build itself. -->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.apache.maven.plugins
                                    </groupId>
                                    <artifactId>
                                        maven-antrun-plugin
                                    </artifactId>
                                    <versionRange>[1.7,)</versionRange>
                                    <goals>
                                        <goal>run</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
     <!-- DIRECTORY CREATION USING MAVEN PLUGIN -  END  -->

</build>
</project>

0 个答案:

没有答案