Spring引导许多对一个非键连接列不起作用

时间:2017-07-21 10:18:55

标签: hibernate spring-boot spring-data spring-data-jpa

我有一个带有两个实体的遗留数据库设计

@Entity
@Table(name = "tblRouters")
public class Router implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "nRouterID")
    private Integer routerId;

    @Column(name = "strRouterName")
    private String routerName;

    @Column(name = "strRouterIP")
    private String routerIp;

    @Column(name = "strLastAutomaticCheckTime")
    private String lastCheckTime;

    @Column(name = "nSupportedSnmpVersion")
    private String protocol;

    @OneToMany(fetch =FetchType.EAGER)
    @JoinColumn(name="strIpAddr", referencedColumnName="strRouterIP")
    //@JoinColumn(name="strIpAddr")
    private Set<RouterDetail> routerDetails;
cons, getters setters....

和RouterDetail类

@Entity
@Table(name = "tblRouter_Detail")
public class RouterDetail implements Serializable {


    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "nObjectId")
    private Integer objectId;

    @Column(name = "cObjectType")
    private Character objectType;

    @Column(name = "strSysName")
    private String systemName;

    @Column(name = "strDescription")
    private String systemDescription;

    @ManyToOne()
    @JoinColumn(name="strRouterIP")
    private Router router;

经过一番努力,我设法创建了正确的映射,允许我在使用Router类中的@JoinColumn中的referencedColumnName后执行查询,但是我需要查询来急切地获取数据所以我去了并改变了这个(在路由器类上)

@OneToMany()

@OneToMany(fetch =FetchType.EAGER)

这使我的代码破坏了以下异常

  

引起:com.microsoft.sqlserver.jdbc.SQLServerException:无效   列名称&#39; strRouterIP&#39;。

我使用spring boot 1.5.3使用此接口执行查询

public interface RouterService extends CrudRepository<Router, Integer> {

    public Iterable<Router> findAllByRouterDetailsObjectType(Character objectType);


}

这种映射有什么问题?

1 个答案:

答案 0 :(得分:1)

我能够通过将RouterDetail连接列修改为此

来解决此问题
@JoinColumn(name ="strIpAddr", referencedColumnName="strRouterIP")