我在映射中做错了什么?’field list’ 中的未知列

huangapple go评论107阅读模式
英文:

What am I mapping incorrectly? Unknown column in the 'field list'

问题

  1. 我正在尝试使用Spring Boot重新创建一个旧的J2EE项目,以此作为学习练习,并且在处理这个映射问题上遇到了一些困难。当我尝试提取`User`实体的结果集时,我一直遇到**在'字段列表'中的未知列'user0_.user_id'**的问题,类似地,对于`Product`实体也是一样的。
  2. 请查看我的模式。
  3. [![我的模式的EER图][1]][1]
  4. [1]: https://i.stack.imgur.com/7WolU.png
  5. 这是我的实体:
  6. ```java
  7. @Entity
  8. public class User {
  9. @Id
  10. @GeneratedValue(strategy = GenerationType.IDENTITY)
  11. private int userId;
  12. private String firstName;
  13. private String lastName;
  14. private String email;
  15. private String username;
  16. private String password;
  17. private String roleName;
  18. @OneToMany(mappedBy = "userJ")
  19. private List<Card> cards;
  20. // 构造函数和getter/setter....
  21. }
  1. @Entity
  2. public class Card {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.AUTO)
  5. private int cardId;
  6. private String cardNumber;
  7. private String cardType;
  8. private String expiryMonth;
  9. private String expiryYear;
  10. private String cardHolder;
  11. @ManyToOne
  12. @JoinColumn(name = "userId")
  13. private User userJ;
  14. @OneToMany(mappedBy = "cardJ")
  15. private List<Purchase> purchases;
  16. // 构造函数和getter/setter....
  17. }
  1. @Entity
  2. public class Purchase {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.AUTO)
  5. private int purchaseId;
  6. private String notes;
  7. @Basic
  8. private Timestamp purchaseDate;
  9. @ManyToOne
  10. @JoinColumn(name = "cardId")
  11. private Card cardJ;
  12. @OneToMany(mappedBy="purchaseJ")
  13. private List<PurchaseItem> purchaseItems;
  14. // 构造函数和getter/setter....
  15. }
  1. @Entity
  2. public class PurchaseItem {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.AUTO)
  5. private int purchaseItemId;
  6. private int quantity;
  7. private BigDecimal price;
  8. @ManyToOne
  9. @JoinColumn(name="purchaseId")
  10. private Purchase purchaseJ;
  11. @ManyToOne
  12. @JoinColumn(name="productId")
  13. private Product productJ;
  14. // 构造函数和getter/setter....
  15. }
  1. @Entity
  2. public class Product {
  3. @Id
  4. private String productId;
  5. private String manufacturer;
  6. private String item;
  7. private String description;
  8. private BigDecimal price;
  9. @Column(nullable = false, columnDefinition = "TINYINT(1)")
  10. private byte available;
  11. @OneToMany(mappedBy = "productJ")
  12. private List<PurchaseItem> purchaseItems;
  13. // 构造函数和getter/setter....
  14. }

我的DAO:

  1. @Repository
  2. @Transactional
  3. public class UserDAOImpl implements UserDAO {
  4. @Autowired
  5. public EntityManager entityManager;
  6. @Override
  7. public List<User> showUsers() {
  8. return entityManager.createQuery("Select u from User u", User.class).getResultList();
  9. }
  10. }

我的Rest Controller:

  1. @RestController
  2. @RequestMapping("/api")
  3. public class SheridanSportsRESTController {
  4. @Autowired
  5. private UserDAO userDAO;
  6. @Autowired
  7. private ProductDAO productDAO;
  8. @GetMapping("/users")
  9. public List<User> showUsers(){
  10. return userDAO.showUsers();
  11. }
  12. @GetMapping("/products")
  13. public List<Product> showProducts(){
  14. return productDAO.showProducts();
  15. }
  16. }

因为PurchaseItem中有额外的属性,我没有在我的关联表上使用@ManyToMany

  1. <details>
  2. <summary>英文:</summary>
  3. I am trying re-create an older J2EE project with Spring Boot as a learning exercise and have been stuck on this mapping issue for a bit. When I try to extract Result Set for my `User` entity, I keep running into **Unknown column &#39;user0_.user_id&#39; in &#39;field list&#39;** and similarly with `Product` entity as well.
  4. Please take a look at my schema.
  5. [![EER diagram for my schema][1]][1]
  6. [1]: https://i.stack.imgur.com/7WolU.png
  7. And here are my entities:

@Entity
public class User {

  1. @Id
  2. @GeneratedValue(strategy = GenerationType.IDENTITY)
  3. private int userId;
  4. private String firstName;
  5. private String lastName;
  6. private String email;
  7. private String username;
  8. private String password;
  9. private String roleName;
  10. @OneToMany(mappedBy = &quot;userJ&quot;)
  11. private List&lt;Card&gt; cards;
  12. //constructors and getters/setters....

}

@Entity
public class Card {

  1. @Id
  2. @GeneratedValue(strategy = GenerationType.AUTO)
  3. private int cardId;
  4. private String cardNumber;
  5. private String cardType;
  6. private String expiryMonth;
  7. private String expiryYear;
  8. private String cardHolder;
  9. @ManyToOne
  10. @JoinColumn(name = &quot;userId&quot;)
  11. private User userJ;
  12. @OneToMany(mappedBy = &quot;cardJ&quot;)
  13. private List&lt;Purchase&gt; purchases;
  14. //constructors and getters/setters....

}

@Entity
public class Purchase {

  1. @Id
  2. @GeneratedValue(strategy = GenerationType.AUTO)
  3. private int purchaseId;
  4. private String notes;
  5. @Basic
  6. private Timestamp purchaseDate;
  7. @ManyToOne
  8. @JoinColumn(name = &quot;cardId&quot;)
  9. private Card cardJ;
  10. @OneToMany(mappedBy=&quot;purchaseJ&quot;)
  11. private List&lt;PurchaseItem&gt; purchaseItems;
  12. //constructors and getters/setters....

}

@Entity
public class PurchaseItem {

  1. @Id
  2. @GeneratedValue(strategy = GenerationType.AUTO)
  3. private int purchaseItemId;
  4. private int quantity;
  5. private BigDecimal price;
  6. @ManyToOne
  7. @JoinColumn(name=&quot;purchaseId&quot;)
  8. private Purchase purchaseJ;
  9. @ManyToOne
  10. @JoinColumn(name=&quot;productId&quot;)
  11. private Product productJ;
  12. //constructors and getters/setters....

}

@Entity
public class Product {

  1. @Id
  2. private String productId;
  3. private String manufacturer;
  4. private String item;
  5. private String description;
  6. private BigDecimal price;
  7. @Column(nullable = false, columnDefinition = &quot;TINYINT(1)&quot;)
  8. private byte available;
  9. @OneToMany(mappedBy = &quot;productJ&quot;)
  10. private List&lt;PurchaseItem&gt; purchaseItems;
  11. //constructors and getters/setters....

}

  1. My DAO:

@Repository
@Transactional
public class UserDAOImpl implements UserDAO {

  1. @Autowired
  2. public EntityManager eman;
  3. @Override
  4. public List&lt;User&gt; showUsers() {
  5. return eman.createQuery(&quot;Select u from User u&quot;, User.class).getResultList();
  6. }

}

  1. My Rest Controller:

@RestController
@RequestMapping("/api")
public class SheridanSportsRESTController {

  1. @Autowired
  2. private UserDAO userDAO;
  3. @Autowired
  4. private ProductDAO productDAO;
  5. @GetMapping(&quot;/users&quot;)
  6. public List&lt;User&gt; showUsers(){
  7. return userDAO.showUsers();
  8. }
  9. @GetMapping(&quot;/products&quot;)
  10. public List&lt;Product&gt; showProducts(){
  11. return productDAO.showProducts();
  12. }

}

  1. And I did not use `@ManyToMany` on my join table because of the extra properties in `PurchaseItem`.
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. Sure, here's the translated content:
  6. 你需要在 userId 属性中加入列注解,就像这样:
  7. ```java
  8. @Column(name="UserId")
  9. private Integer userId;

这是因为在被注解为 @Entity 的类中,如果你不对列进行注解,且它符合 Java 标准的驼峰命名规则,那么 userId 会被转换为 user_id,并添加下划线 ()。

由于你的列名不是那样命名的,你需要使用 @Column 注解来指定列名。

我建议你将 id 定义为非基本数据类型,因为你可能需要将其值设为 null。

英文:

You have to put the column annotation in the userId property like this:

  1. @Column(name=&quot;UserId&quot;)
  2. private Integer userId;

This is because when in a class annotated as @Entity, when you do not annotate a column, if it is camelcase according to the java standard, it transforms userId into user_id, adding underscores ().

Since your column is not named like that, you need to specify the name with the @Column annotation.

I recommend that you define the id as a non-primitive data type because you will need to set the value null.

答案2

得分: 1

我已经能够通过在我的 application.properties 文件中更改命名策略来解决这个问题,正如在 SQL 错误:1054,SQLState:42S22“字段列表”中的未知列错误 Java Spring Boot Mysql 错误 中所提到的。

感谢大家的帮助和关键信息。

干杯 我在映射中做错了什么?’field list’ 中的未知列

英文:

I was able to resolve the issue by changing the naming strategy in my application.properties file as mentioned in SQL Error: 1054, SQLState: 42S22 Unknown column in 'field list' error Java Spring Boot Mysql error

Thank you all for your help and the vital information regardless.

Cheers 我在映射中做错了什么?’field list’ 中的未知列

huangapple
  • 本文由 发表于 2020年8月14日 21:24:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63413656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定