Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用Spring数据JPA创建联接_Java_Jpa_Spring Data - Fatal编程技术网

Java 无法使用Spring数据JPA创建联接

Java 无法使用Spring数据JPA创建联接,java,jpa,spring-data,Java,Jpa,Spring Data,我是Spring数据新手,尝试通过以下教程和其他一些教程来解决这个问题,但没有太大成功 我试图在两个表之间使用Spring数据JPA进行简单的连接。 数据库中的表称为: *用户\车辆->包含每个用户所有车辆的信息,因为一个用户可以有许多车辆 *vehicle_model,包含有关车型id、名称等的数据 用户车辆表中数据库中的当前数据: ID |车辆ID |用户ID 1 | 1 | 1 2 | 2 | 1 下面是我尝试过但无法使用的代码,从帖子中删除getter和setter以

我是Spring数据新手,尝试通过以下教程和其他一些教程来解决这个问题,但没有太大成功

我试图在两个表之间使用Spring数据JPA进行简单的连接。 数据库中的表称为: *用户\车辆->包含每个用户所有车辆的信息,因为一个用户可以有许多车辆 *vehicle_model,包含有关车型id、名称等的数据

用户车辆表中数据库中的当前数据: ID |车辆ID |用户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);
}
我希望在充满车辆模型数据的iterable中得到2个结果。相反,我得到了2个结果,但对于vehicleModels属性,我无法计算表达式方法抛出的“org.hibernate.exception.sqlgrammareexception”异常

以下是控制台的输出:

2019-06-23 02:04:10.988 DEBUG 5896-[nio-8080-exec-1]org.hibernate.SQL:从user_vehicle USERVICTOR 0_u0选择USERVICTOR 0.id作为id1,USERVICTOR 0.user_0.id作为user_2_1,USERVICTOR_0.user_0.id作为USERVICTOR_3_1,其中USERVICTOR user_0.user_0.id=? 2019-06-23 02:04:11.034调试5896-[nio-8080-exec-1]org.hibernate.SQL:选择vehiclemod0.vehiclemod0.id作为vehicle\u 9\u 4\u 0\u,vehiclemod0.id作为id1\u 4\u,vehiclemod0.ccm作为ccm2\u 4\u 1\u,vehiclemod0.id作为engine\u\u\u类型\u id作为engine\u\u\u\u\u\u\u 4\u 4\u 1\u,Vehiclemodu激活,车辆型号0.kw为kw5\u 4\u 1\u,车辆型号0.id为制造商6\u 4\u 1\u,车辆型号0.title7\u 4\u 1\u,车辆型号0.id为车辆9\u 4\u 1\u,车辆型号0.id为车辆8\u 4\u 1\u制造年份,车辆型号0.id=? 2019-06-23 02:04:11.035 WARN 5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:1054,SQLState:42S22 2019-06-23 02:04:11.035错误5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:字段列表中的未知列“vehiclemod0.vehicle\u id” 2019-06-23 02:04:11.036调试5896-[nio-8080-exec-1]org.hibernate.SQL:选择vehiclemod0.vehiclemod0.id作为vehicle\u9\u4\u0,vehiclemod0.id作为id1\u4\u0,vehiclemod0.ccm作为ccm2\u4\u1,vehiclemod0.id作为engine\u3\u4\u0,vehiclemod0.id作为engine\u4\u1,VehicleModu激活,车辆型号0.kw为kw5\u 4\u 1\u,车辆型号0.id为制造商6\u 4\u 1\u,车辆型号0.title7\u 4\u 1\u,车辆型号0.id为车辆9\u 4\u 1\u,车辆型号0.id为车辆8\u 4\u 1\u制造年份,车辆型号0.id=? 2019-06-23 02:04:11.037警告5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:1054,SQLState:42S22 2019-06-23 02:04:11.037错误5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:字段列表中的未知列“vehiclemod0.vehicle\u id” 2019-06-23 02:04:11.038调试5896-[nio-8080-exec-1]org.hibernate.SQL:选择vehiclemod0.vehiclemod0.id作为vehicle\u9\u4\u0,vehiclemod0.id作为id1\u4\u0,vehiclemod0.ccm作为ccm2\u4\u1,vehiclemod0.id作为engine\u3\u4\u0,vehiclemod0.id作为engine\u4\u1,VehicleModu激活,车辆型号0.kw为kw5\u 4\u 1\u,车辆型号0.id为制造商6\u 4\u 1\u,车辆型号0.title7\u 4\u 1\u,车辆型号0.id为车辆9\u 4\u 1\u,车辆型号0.id为车辆8\u 4\u 1\u制造年份,车辆型号0.id=? 2019-06-23 02:04:11.039警告5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:1054,SQLState:42S22 2019-06-23 02:04:11.040错误5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:字段列表中的未知列“vehiclemod0.vehicle\u id” 2019-06-23 02:04:11.042调试5896-[nio-8080-exec-1]org.hibernate.SQL:选择vehiclemod0.vehiclemod0.id作为vehicle\u 9\u 4\u 0\u,vehiclemod0.id作为id1\u 4\u,vehiclemod0.ccm作为ccm2\u 4\u 1\u,vehiclemod0.com作为发动机类型\u id作为发动机\u t3\u 4\u 1\u,vehiclemod0.id作为激活,车辆型号0.kw为kw5\u 4\u 1\u,车辆型号0.id为制造商6\u 4\u 1\u,车辆型号0.title7\u 4\u 1\u,车辆型号0.id为车辆9\u 4\u 1\u,车辆型号0.id为车辆8\u 4\u 1\u制造年份,车辆型号0.id=? 2019-06-23 02:04:11.043警告5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:1054,SQLState:42S22 2019-06-23 02:04:11.043错误5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:字段列表中的未知列“vehiclemod0.vehicle\u id” 2019-06-23 02:04:11.045调试5896-[nio-8080-exec-1]org.hibernate.SQL:选择vehiclemod0.vehiclemod0.id作为vehicle\u 9\u 4\u 0\u,vehiclemod0.id作为id1\u 4\u 0\u,vehiclemod0.id作为id1\u 4\u 1\u,vehiclemo d0.ccm为ccm2、车辆模块0、发动机类型id为发动机t3、车辆模块0、激活状态为激活状态为激活状态为激活状态为激活状态为激活状态4、车辆模块0、功率为kw5、车辆模块0、制造商id为制造商id为制造商id为制造商id、车辆id为标题7、车辆id为车辆,车辆模块0.车辆制造年份作为车辆模块0的车辆8.车辆4.车辆1.制造年份,其中车辆模块0.车辆id=? 2019-06-23 02:04:11.046 WARN 5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:1054,SQLState:42S22 2019-06-23 02:04:11.046错误5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:字段列表中的未知列“vehiclemod0.vehicle\u id” 2019-06-23 02:04:11.048调试5896-[nio-8080-exec-1]org.hibernate.SQL:选择vehiclemod0.vehiclemod0.id作为vehicle\u 9\u 4\u 0\u,vehiclemod0.id作为id1\u 4\u,vehiclemod0.ccm作为ccm2\u 4\u 1\u,vehiclemod0.com作为发动机类型\u id作为发动机\u t3\u 4\u 1\u,vehiclemod0作为激活,车辆型号0.kw为kw5\u 4\u 1\u,车辆型号0.id为制造商6\u 4\u 1\u,车辆型号0.title7\u 4\u 1\u,车辆型号0.id为车辆9\u 4\u 1\u,车辆型号0.id为车辆8\u 4\u 1\u制造年份,车辆型号0.id=? 2019-06-23 02:04:11.049警告5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:1054,SQLState:42S22 2019-06-23 02:04:11.049错误5896-[nio-8080-exec-1]o.h.engine.jdbc.spi.SqlExceptionHelper:字段列表中的未知列“vehiclemod0.vehicle\u id” 在这里找到了解决方案:

我一直觉得有点不对,因为这是一段多人的关系。我的想法是转到中间的表,选择所有车辆id,其中userId=:id,并从车辆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;
问题不是我真的不希望结果中包含用户对象,所以现在我需要弄清楚这一点。在那之后,我将尝试映射VehicleManufacturer类,以在相同的响应中获得这些结果。

在这里找到了一个解决方案:

我一直觉得有点不对,因为这是一段多人的关系。我的想法是转到中间的表,选择所有车辆id,其中userId=:id,并从车辆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;

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

以下是我有意想要的解决方案-根据user\u vehicle表中的用户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);
现在这是一段“一夫一妻制”的关系,而且正在做我第一次真正想要做的事情


希望它能帮助某人

以下是我有意想要的解决方案-根据用户车辆表中的用户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);
现在这是一段“一夫一妻制”的关系,而且正在做我第一次真正想要做的事情


希望它能帮助别人

你确定你的onetomany映射是正确的吗?因为外键在用户\车辆表上。您确定您的onetomany映射正确吗?因为外键在用户\车辆表上。
@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;