简体   繁体   English

Spring JPA-OneToOne不从辅助表中选择数据-MySQL

[英]Spring jpa - oneToOne not selecting data from secondary table - mySql

New to spring data and have an issue I can't resolve. Spring数据的新手,有一个我无法解决的问题。 Have a mySql database and am trying to use OneToOne to get data from a table using a foreign key. 有一个mySql数据库,并尝试使用OneToOne通过外键从表中获取数据。

Table 1 - location
   id      int(11) pk nn UQ
   zipcode varchar(11) NN UQ
   city    varchar(45) NN
   lat     Double      NN
   lon     Double      NN

Table 2 - weather
   id           INT(11)  PK NN
   location_id  INT(11)  NN
   desc         varchar(45)  NN
Foreign Key name fk_location_id  referenced table location column location_id referenced column id - pointed to location column id.`

WeatherData.java column definition @OneToOne (fetch = FetchType.LAZY) @JoinColumn(name="fk_location_id") private LocationData location; WeatherData.java列定义@OneToOne (fetch = FetchType.LAZY) @JoinColumn(name="fk_location_id") private LocationData location;

Code performing the query: for (WeatherData weather : repository.findAll()) { log.info(weather.toString()); } 执行查询的代码: for (WeatherData weather : repository.findAll()) { log.info(weather.toString()); } for (WeatherData weather : repository.findAll()) { log.info(weather.toString()); }

` Set a break point in the code above to the log.info line and look at the weather record returned. 在上面的代码中的log.info行中设置一个断点,并查看返回的气象记录。 The location value is null. 位置值为空。 Have tried setting the OneToOne column name to id without success. 尝试将OneToOne列名设置为id失败。

What am I doing wrong? 我究竟做错了什么?

I'd try the following 3 things: 我会尝试以下3件事:

  1. set the value in your @JoinColumn to the name of the FK column. 将@JoinColumn中的值设置为FK列的名称。
  2. make sure your FK entity class (LocationData) has a properly formatted @OneToOne annotation. 确保您的FK实体类(LocationData)具有格式正确的@OneToOne批注。 The mappedBy value is the name of the field in WeatherData that has the @OneToOne and @JoinColumn annotations. mapledBy值是WeatherData中具有@OneToOne和@JoinColumn批注的字段的名称。
  3. make sure your entity classes are annotated as entities (via @Entity). 确保将您的实体类注释为实体(通过@Entity)。

Using the this site's example , your weather table would be the book table, and location would be the book_detail table. 使用此站点的示例 ,您的天气表将是book表,而位置将是book_detail表。 That being said, there are a few annotations to make sure you're using. 就是说,有一些注释可以确保您正在使用。 First of all, make sure your entity classes are labeled as so. 首先,请确保您的实体类已被标记。 ie: 即:

 @Entity
 @Table(name = "weather")
 public class WeatherData {

If your entity class and table have the same name, then you can omit the @Table annotation; 如果您的实体类和表具有相同的名称,则可以省略@Table批注; however, I like to always add it as I consider it good practice. 但是,我总是喜欢添加它,因为我认为这是一种很好的做法。 Make sure you annotate all your entity classes, not just WeatherData. 确保注释所有实体类,而不仅是WeatherData。 Now, as for the location_id, your @JoinColumn should refer to the column in your table. 现在,关于location_id,您的@JoinColumn应该引用表中的列。

@OneToOne (fetch = FetchType.LAZY)
@JoinColumn(name="location_id")
private LocationData location;

Then, in your LocationData class, you need to annotate the locationId field with @OneToOne as well. 然后,在LocationData类中,还需要使用@OneToOne注释locationId字段。

@OneToOne(mappedBy = "location")
private WeatherData weather;

Take a look at the linked article for more info. 查看链接文章以了解更多信息。 I hope this helps. 我希望这有帮助。

Fixed the issue with the following changes: 通过以下更改解决了该问题:

@OneToOne (fetch = FetchType.EAGER) @JoinColumn(name="location_id") private LocationData location;

and fixed incorrect data type defined as a int when it should have been a string in the locationdata class. 并修复了在locationdata类中应该为字符串的错误数据类型定义为int的问题。 Also had an issue with a double set as a int in the locationdata toString method. 在locationdata toString方法中将double设置为int时也遇到了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM