naoru
naoru

Reputation: 2227

Spring boot Many to one with non key join column not working

I have a legacy database design with two entities

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

and the RouterDetail class

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

After some struggle i managed to create the correct mapping that allowed me to execute the query after using the referencedColumnName in the @JoinColumn in Router class, however i need the query to be fetch the data eagerly so I went and changed this (on the Router class)

@OneToMany()

to

@OneToMany(fetch =FetchType.EAGER)

this broke my code with the following exception

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'strRouterIP'.

im using spring boot 1.5.3 with this interface to execute the query

public interface RouterService extends CrudRepository<Router, Integer> {

    public Iterable<Router> findAllByRouterDetailsObjectType(Character objectType);


}

What is wrong with this mapping?

Upvotes: 2

Views: 2749

Answers (1)

naoru
naoru

Reputation: 2227

I was able to solve this issue by modifying the RouterDetail join column to this

@JoinColumn(name ="strIpAddr", referencedColumnName="strRouterIP")

Upvotes: 3

Related Questions