无法使用Spring Data JPA创建JOIN

时间:2019-06-23 00:07:24

标签: java jpa spring-data

我是Spring Data的新手,并试图通过遵循this post on SO和其他一些教程来解决此问题,但没有成功。

我正在尝试使用Spring Data JPA在2个表之间进行简单的Join。 数据库中的表称为: * user_vehicle->包含有关每个用户的所有车辆的信息,因为1个用户可以拥有许多车辆 * vehicle_model,其中包含有关车辆型号的数据(ID,名称等)

user_vehicle表中数据库中的当前数据:
ID | vehicle_id | user_id
1 | 1 | 1
2 | 2 | 1

这是我尝试过的代码,但无法正常工作(从帖子中删除了getter和setter可以简化代码):

@Entity(name = "vehicle_model")
public class VehicleModel {

@Id
@Min(1)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long manufacturerId;
private String title;
private int ccm;
private int kw;
private int yearOfManufacture;
private int engineTypeId;
private boolean isActive;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vehicle_id", insertable = false, updatable = false)
@Fetch(FetchMode.JOIN)
private UserVehicle userVehicle;
}


@Entity(name = "user_vehicle")
public class UserVehicle {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private long vehicleId;
@Column(nullable = false)
private long userId;

@OneToMany(targetEntity = VehicleModel.class, mappedBy = "userVehicle", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
List<VehicleModel> vehicleModels;
}


@Repository
public interface UserVehicleRepository extends CrudRepository<UserVehicle, Long> 
{
    Iterable<UserVehicle> findVehicleModelsByUserId(Long userId);
}

我期望在填充了vehicle_model数据的迭代中获得2个结果。取而代之的是,我得到2个结果,但是对于vehicleModels属性,我得到了“无法评估方法Method抛出了'org.hibernate.exception.SQLGrammarException'异常。”

这是控制台的输出:

2019-06-23 02:04:10.988 DEBUG 5896 --- [nio-8080-exec-1] org.hibernate.SQL : select uservehicl0_.id as id1_1_, uservehicl0_.user_id as user_id2_1_, uservehicl0_.vehicle_id as vehicle_3_1_ from user_vehicle uservehicl0_ where uservehicl0_.user_id=? 2019-06-23 02:04:11.034 DEBUG 5896 --- [nio-8080-exec-1] org.hibernate.SQL : select vehiclemod0_.vehicle_id as vehicle_9_4_0_, vehiclemod0_.id as id1_4_0_, vehiclemod0_.id as id1_4_1_, vehiclemod0_.ccm as ccm2_4_1_, vehiclemod0_.engine_type_id as engine_t3_4_1_, vehiclemod0_.is_active as is_activ4_4_1_, vehiclemod0_.kw as kw5_4_1_, vehiclemod0_.manufacturer_id as manufact6_4_1_, vehiclemod0_.title as title7_4_1_, vehiclemod0_.vehicle_id as vehicle_9_4_1_, vehiclemod0_.year_of_manufacture as year_of_8_4_1_ from vehicle_model vehiclemod0_ where vehiclemod0_.vehicle_id=? 2019-06-23 02:04:11.035 WARN 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22 2019-06-23 02:04:11.035 ERROR 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'vehiclemod0_.vehicle_id' in 'field list' 2019-06-23 02:04:11.036 DEBUG 5896 --- [nio-8080-exec-1] org.hibernate.SQL : select vehiclemod0_.vehicle_id as vehicle_9_4_0_, vehiclemod0_.id as id1_4_0_, vehiclemod0_.id as id1_4_1_, vehiclemod0_.ccm as ccm2_4_1_, vehiclemod0_.engine_type_id as engine_t3_4_1_, vehiclemod0_.is_active as is_activ4_4_1_, vehiclemod0_.kw as kw5_4_1_, vehiclemod0_.manufacturer_id as manufact6_4_1_, vehiclemod0_.title as title7_4_1_, vehiclemod0_.vehicle_id as vehicle_9_4_1_, vehiclemod0_.year_of_manufacture as year_of_8_4_1_ from vehicle_model vehiclemod0_ where vehiclemod0_.vehicle_id=? 2019-06-23 02:04:11.037 WARN 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22 2019-06-23 02:04:11.037 ERROR 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'vehiclemod0_.vehicle_id' in 'field list' 2019-06-23 02:04:11.038 DEBUG 5896 --- [nio-8080-exec-1] org.hibernate.SQL : select vehiclemod0_.vehicle_id as vehicle_9_4_0_, vehiclemod0_.id as id1_4_0_, vehiclemod0_.id as id1_4_1_, vehiclemod0_.ccm as ccm2_4_1_, vehiclemod0_.engine_type_id as engine_t3_4_1_, vehiclemod0_.is_active as is_activ4_4_1_, vehiclemod0_.kw as kw5_4_1_, vehiclemod0_.manufacturer_id as manufact6_4_1_, vehiclemod0_.title as title7_4_1_, vehiclemod0_.vehicle_id as vehicle_9_4_1_, vehiclemod0_.year_of_manufacture as year_of_8_4_1_ from vehicle_model vehiclemod0_ where vehiclemod0_.vehicle_id=? 2019-06-23 02:04:11.039 WARN 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22 2019-06-23 02:04:11.040 ERROR 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'vehiclemod0_.vehicle_id' in 'field list' 2019-06-23 02:04:11.042 DEBUG 5896 --- [nio-8080-exec-1] org.hibernate.SQL : select vehiclemod0_.vehicle_id as vehicle_9_4_0_, vehiclemod0_.id as id1_4_0_, vehiclemod0_.id as id1_4_1_, vehiclemod0_.ccm as ccm2_4_1_, vehiclemod0_.engine_type_id as engine_t3_4_1_, vehiclemod0_.is_active as is_activ4_4_1_, vehiclemod0_.kw as kw5_4_1_, vehiclemod0_.manufacturer_id as manufact6_4_1_, vehiclemod0_.title as title7_4_1_, vehiclemod0_.vehicle_id as vehicle_9_4_1_, vehiclemod0_.year_of_manufacture as year_of_8_4_1_ from vehicle_model vehiclemod0_ where vehiclemod0_.vehicle_id=? 2019-06-23 02:04:11.043 WARN 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22 2019-06-23 02:04:11.043 ERROR 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'vehiclemod0_.vehicle_id' in 'field list' 2019-06-23 02:04:11.045 DEBUG 5896 --- [nio-8080-exec-1] org.hibernate.SQL : select vehiclemod0_.vehicle_id as vehicle_9_4_0_, vehiclemod0_.id as id1_4_0_, vehiclemod0_.id as id1_4_1_, vehiclemod0_.ccm as ccm2_4_1_, vehiclemod0_.engine_type_id as engine_t3_4_1_, vehiclemod0_.is_active as is_activ4_4_1_, vehiclemod0_.kw as kw5_4_1_, vehiclemod0_.manufacturer_id as manufact6_4_1_, vehiclemod0_.title as title7_4_1_, vehiclemod0_.vehicle_id as vehicle_9_4_1_, vehiclemod0_.year_of_manufacture as year_of_8_4_1_ from vehicle_model vehiclemod0_ where vehiclemod0_.vehicle_id=? 2019-06-23 02:04:11.046 WARN 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22 2019-06-23 02:04:11.046 ERROR 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'vehiclemod0_.vehicle_id' in 'field list' 2019-06-23 02:04:11.048 DEBUG 5896 --- [nio-8080-exec-1] org.hibernate.SQL : select vehiclemod0_.vehicle_id as vehicle_9_4_0_, vehiclemod0_.id as id1_4_0_, vehiclemod0_.id as id1_4_1_, vehiclemod0_.ccm as ccm2_4_1_, vehiclemod0_.engine_type_id as engine_t3_4_1_, vehiclemod0_.is_active as is_activ4_4_1_, vehiclemod0_.kw as kw5_4_1_, vehiclemod0_.manufacturer_id as manufact6_4_1_, vehiclemod0_.title as title7_4_1_, vehiclemod0_.vehicle_id as vehicle_9_4_1_, vehiclemod0_.year_of_manufacture as year_of_8_4_1_ from vehicle_model vehiclemod0_ where vehiclemod0_.vehicle_id=? 2019-06-23 02:04:11.049 WARN 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22 2019-06-23 02:04:11.049 ERROR 5896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'vehiclemod0_.vehicle_id' in 'field list'

2 个答案:

答案 0 :(得分:0)

在这里找到了解决方案:https://www.baeldung.com/jpa-many-to-many

由于这是一个ManyToMany关系,所以我一直觉得有点不对。我的想法是转到“中间”表,然后从vehicle_ids列表中选择userId =:id的所有vehicle_id,以获取每辆车的详细信息。

这是有效的正确实现:

@Entity(name = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String firstName;
private String lastName;
private boolean isActive;

@OneToMany(mappedBy = "user")
private Set<UserVehicle> userVehicles;

@Entity(name = "user_vehicle")
public class UserVehicle {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private boolean isActive;

@ManyToOne
@JoinColumn(name = "vehicle_id")
private Vehicle vehicle;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;


@Entity(name = "vehicle_model")
public class Vehicle {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long manufacturerId;
private String title;
private int ccm;
private int kw;
private int yearOfManufacture;
private int engineTypeId;
private boolean isActive;

@OneToMany(mappedBy = "vehicle")
private Set<UserVehicle> userVehicles;

最后,在存储库接口中,我有一个像这样的方法:

public interface UserVehicleRepository extends CrudRepository<UserVehicle, Long> {

Iterable<UserVehicle> findVehicleModelsByUserId(Long userId);

输出如下:

[
    {
        "id": 2,
        "vehicle": {
            "id": 2,
            "manufacturerId": 1,
            "title": "Fazer FZ1S",
            "ccm": 998,
            "kw": 110,
            "yearOfManufacture": 2008,
            "engineTypeId": 1,
            "active": true
        },
        "user": {
            "id": 2,
            "username": "sfajkovic",
            "password": "33",
            "firstName": "Ivan",
            "lastName": "Fajkovic",
            "active": true
        },
        "active": true
    }
]

问题不是我真的不想要结果中的用户对象,所以现在我需要弄清楚那个对象。然后,我将尝试映射VehicleManufacturer类在相同的响应中也获得这些结果。

答案 1 :(得分:0)

这是解决我想要做的事情的方法-根据user_vehicle表中的user_id搜索车辆:

@Entity(name = "user_vehicle")
public class UserVehicle {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;
  private boolean isActive;
  private long userId;

  @ManyToOne
  @JoinColumn(name = "vehicle_id")
  private Vehicle vehicle;


@Entity(name = "vehicle_model")
public class Vehicle {

  @Id
  @Min(1)
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private Long manufacturerId;
  private String title;
  private int ccm;
  private int kw;
  private int yearOfManufacture;
  private int engineTypeId;
  private boolean isActive;

  @OneToMany(mappedBy = "vehicle")
  private Set<UserVehicle> userVehicles;


@Repository
public interface UserVehicleRepository extends CrudRepository<UserVehicle, Long> {

  Iterable<UserVehicle> findVehicleModelsByUserId(Long userId);

实际上根本不使用User类:

@Entity(name = "user")
public class User {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String username;
  private String password;
  private String firstName;
  private String lastName;
  private boolean isActive;

现在这是一个一对多的关系,并且是我第一次真正要做的事情。

希望它对某人有帮助