英文:
What am I mapping incorrectly? Unknown column in the 'field list'
问题
我正在尝试使用Spring Boot重新创建一个旧的J2EE项目,以此作为学习练习,并且在处理这个映射问题上遇到了一些困难。当我尝试提取`User`实体的结果集时,我一直遇到**在'字段列表'中的未知列'user0_.user_id'**的问题,类似地,对于`Product`实体也是一样的。
请查看我的模式。
[![我的模式的EER图][1]][1]
[1]: https://i.stack.imgur.com/7WolU.png
这是我的实体:
```java
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int userId;
private String firstName;
private String lastName;
private String email;
private String username;
private String password;
private String roleName;
@OneToMany(mappedBy = "userJ")
private List<Card> cards;
// 构造函数和getter/setter....
}
@Entity
public class Card {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int cardId;
private String cardNumber;
private String cardType;
private String expiryMonth;
private String expiryYear;
private String cardHolder;
@ManyToOne
@JoinColumn(name = "userId")
private User userJ;
@OneToMany(mappedBy = "cardJ")
private List<Purchase> purchases;
// 构造函数和getter/setter....
}
@Entity
public class Purchase {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int purchaseId;
private String notes;
@Basic
private Timestamp purchaseDate;
@ManyToOne
@JoinColumn(name = "cardId")
private Card cardJ;
@OneToMany(mappedBy="purchaseJ")
private List<PurchaseItem> purchaseItems;
// 构造函数和getter/setter....
}
@Entity
public class PurchaseItem {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int purchaseItemId;
private int quantity;
private BigDecimal price;
@ManyToOne
@JoinColumn(name="purchaseId")
private Purchase purchaseJ;
@ManyToOne
@JoinColumn(name="productId")
private Product productJ;
// 构造函数和getter/setter....
}
@Entity
public class Product {
@Id
private String productId;
private String manufacturer;
private String item;
private String description;
private BigDecimal price;
@Column(nullable = false, columnDefinition = "TINYINT(1)")
private byte available;
@OneToMany(mappedBy = "productJ")
private List<PurchaseItem> purchaseItems;
// 构造函数和getter/setter....
}
我的DAO:
@Repository
@Transactional
public class UserDAOImpl implements UserDAO {
@Autowired
public EntityManager entityManager;
@Override
public List<User> showUsers() {
return entityManager.createQuery("Select u from User u", User.class).getResultList();
}
}
我的Rest Controller:
@RestController
@RequestMapping("/api")
public class SheridanSportsRESTController {
@Autowired
private UserDAO userDAO;
@Autowired
private ProductDAO productDAO;
@GetMapping("/users")
public List<User> showUsers(){
return userDAO.showUsers();
}
@GetMapping("/products")
public List<Product> showProducts(){
return productDAO.showProducts();
}
}
因为PurchaseItem
中有额外的属性,我没有在我的关联表上使用@ManyToMany
。
<details>
<summary>英文:</summary>
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 'user0_.user_id' in 'field list'** and similarly with `Product` entity as well.
Please take a look at my schema.
[![EER diagram for my schema][1]][1]
[1]: https://i.stack.imgur.com/7WolU.png
And here are my entities:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int userId;
private String firstName;
private String lastName;
private String email;
private String username;
private String password;
private String roleName;
@OneToMany(mappedBy = "userJ")
private List<Card> cards;
//constructors and getters/setters....
}
@Entity
public class Card {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int cardId;
private String cardNumber;
private String cardType;
private String expiryMonth;
private String expiryYear;
private String cardHolder;
@ManyToOne
@JoinColumn(name = "userId")
private User userJ;
@OneToMany(mappedBy = "cardJ")
private List<Purchase> purchases;
//constructors and getters/setters....
}
@Entity
public class Purchase {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int purchaseId;
private String notes;
@Basic
private Timestamp purchaseDate;
@ManyToOne
@JoinColumn(name = "cardId")
private Card cardJ;
@OneToMany(mappedBy="purchaseJ")
private List<PurchaseItem> purchaseItems;
//constructors and getters/setters....
}
@Entity
public class PurchaseItem {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int purchaseItemId;
private int quantity;
private BigDecimal price;
@ManyToOne
@JoinColumn(name="purchaseId")
private Purchase purchaseJ;
@ManyToOne
@JoinColumn(name="productId")
private Product productJ;
//constructors and getters/setters....
}
@Entity
public class Product {
@Id
private String productId;
private String manufacturer;
private String item;
private String description;
private BigDecimal price;
@Column(nullable = false, columnDefinition = "TINYINT(1)")
private byte available;
@OneToMany(mappedBy = "productJ")
private List<PurchaseItem> purchaseItems;
//constructors and getters/setters....
}
My DAO:
@Repository
@Transactional
public class UserDAOImpl implements UserDAO {
@Autowired
public EntityManager eman;
@Override
public List<User> showUsers() {
return eman.createQuery("Select u from User u", User.class).getResultList();
}
}
My Rest Controller:
@RestController
@RequestMapping("/api")
public class SheridanSportsRESTController {
@Autowired
private UserDAO userDAO;
@Autowired
private ProductDAO productDAO;
@GetMapping("/users")
public List<User> showUsers(){
return userDAO.showUsers();
}
@GetMapping("/products")
public List<Product> showProducts(){
return productDAO.showProducts();
}
}
And I did not use `@ManyToMany` on my join table because of the extra properties in `PurchaseItem`.
</details>
# 答案1
**得分**: 1
Sure, here's the translated content:
你需要在 userId 属性中加入列注解,就像这样:
```java
@Column(name="UserId")
private Integer userId;
这是因为在被注解为 @Entity 的类中,如果你不对列进行注解,且它符合 Java 标准的驼峰命名规则,那么 userId 会被转换为 user_id,并添加下划线 ()。
由于你的列名不是那样命名的,你需要使用 @Column 注解来指定列名。
我建议你将 id 定义为非基本数据类型,因为你可能需要将其值设为 null。
英文:
You have to put the column annotation in the userId property like this:
@Column(name="UserId")
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 错误 中所提到的。
感谢大家的帮助和关键信息。
干杯
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论