I have Table User in SQL Server DB with close to 30 columns. I have also created an JPA Entity for corresponding DB Table. The JPA entity looks something like this:
@Entity
@Table(name="USER")
@Setter
@Getter
public class User{
@Id
@Column(name="ID",nullable=false)
private Integer id;
@Column(name="NAME")
private String name;
@Column(name="ADDRESS")
private String address;
@Column(name="FATHER_NAME")
private String fathersName;
}
Now I am trying to create a native query which will only fetch id and name from the User Table. The Repository class looks something like this.
@Repository
public interface UserDao extends JpaRepository<User,Integer>{
@Query(value=" SELECT u.ID,u.NAME from USER u", nativeQuery=true)
List<User> fetchUser();
}
Now when this is being executed, it is throwing an exception as below:
com.microsoft.sqlserver.jdbc.SqlServerException: The column name address is not valid
When I add the address column to my query this exception goes but then it come for another field.
com.microsoft.sqlserver.jdbc.SqlServerException: The column name father_name is not valid
So, now I want to modify the JPA Entity in such a way that this fields became optional. I have tried using @Transient but if some other query is using this address field then it will come as null over there. I have also tried all the below annotations:
- @Basic
- @NotFound(action=NotFoundAction.IGNORE)
- @Column(nullable="true")
- @JsonProperty
But none of this annotations work for me.