如何使用Spring框架

时间:2016-12-04 06:50:04

标签: java hibernate spring-mvc

我正在尝试使用spring框架配置Hibernate OneToOne映射,但无法正确完成。

我定义了3个模型,接口:User.java,Organization.java,OrganizationUser.java及其实现UserImplemenatation.java,OrganizationImplementation.java和OrganizationUserImplementation.java。每个模型都有以下字段

User: userId, userName
Organization: organziationId, organziationName
OrganziationUser: organziationId, userId, roleId

一开始工作正常。 Spring框架中的配置如下所示:

<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="hibernate4AnnotatedSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>model.implementation.OrganizationImplementation</value>
            <value>model.implementation.UserImplementation</value>
            <value>model.implementation.UserSessionImplementation</value>
            <value>model.implementation.OrganizationUserImplementation</value>
            <value>model.implementation.OrganizationInvitationImplementation</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

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

<import resource="actions.xml"/>

<import resource="services.xml"/>

<import resource="dao.xml"/>

然后,我想在OrganizationUserImplementation.java中添加OneToOne映射,以获取userName和organizationName。所以我在OrganziationUserImplementation.java中添加了两个属性User user和Organization organziation:

package model.implementation;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotBlank;

import model.Organization;
import model.OrganizationUser;
import model.User;

@Entity
@Table(name="Organization_User")
public class OrganizationUserImplementation implements OrganizationUser {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name = "organization_id")
    @NotNull
    @NotBlank
    private String organizationId;

    @Column(name = "user_id")
    @NotNull
    @NotBlank
    private String userId;

    @Column(name = "role_id")
    @NotNull
    @NotBlank
    private int roleId;

    @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinTable(name="Organization_User",
    joinColumns={@JoinColumn(name="userId", referencedColumnName="user_id")},
    inverseJoinColumns={@JoinColumn(name="userId", referencedColumnName="user_id")})
    private User user;

    @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinTable(name="Organization_User",
    joinColumns={@JoinColumn(name="organizationId", referencedColumnName="organization_id")},
    inverseJoinColumns={@JoinColumn(name="organizationId", referencedColumnName="organization_id")})
    private Organization organization;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getOrganizationId() {
        return organizationId;
    }

    public void setOrganizationId(String organizationId) {
        this.organizationId = organizationId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getRoleId() {
        return roleId;
    }

    public void setRoleId(int roleId) {
        this.roleId = roleId;
    }

}

然后,当我运行我的项目时,我收到错误消息:

使用名称&#39; hibernate4AnnotatedSessionFactory&#39;创建bean时出错。在ServletContext资源[/WEB-INF/database.xml]中定义:init方法的调用失败;嵌套异常是org.hibernate.AnnotationException:@OneToOne或@ManyToOne on model.implementation.OrganizationUserImplementation.organization引用一个未知实体:model.Organization

我想我错过了某处的配置。因为Hibernate认为proerty organziation是接口模型.Oganganiation不是实现model.implementation.OrganizationImplementation。但我已经用Google搜索了一整天,尝试找到有关此配置的教程。但我能找到的是OneToOne的休眠配置。有谁知道我应该如何在spring框架中配置它?

非常感谢。

0 个答案:

没有答案