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

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

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 &#39;user0_.user_id&#39; in &#39;field list&#39;** 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 = &quot;userJ&quot;)
private List&lt;Card&gt; 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 = &quot;userId&quot;)
private User userJ;

@OneToMany(mappedBy = &quot;cardJ&quot;)
private List&lt;Purchase&gt; 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 = &quot;cardId&quot;)
private Card cardJ;

@OneToMany(mappedBy=&quot;purchaseJ&quot;)
private List&lt;PurchaseItem&gt; 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=&quot;purchaseId&quot;)
private Purchase purchaseJ;

@ManyToOne
@JoinColumn(name=&quot;productId&quot;)
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 = &quot;TINYINT(1)&quot;)
private byte available;

@OneToMany(mappedBy = &quot;productJ&quot;)
private List&lt;PurchaseItem&gt; purchaseItems;

//constructors and getters/setters....

}

My DAO:

@Repository
@Transactional
public class UserDAOImpl implements UserDAO {

@Autowired
public EntityManager eman;

@Override
public List&lt;User&gt; showUsers() {
	
	return eman.createQuery(&quot;Select u from User u&quot;, User.class).getResultList();
}

}

My Rest Controller:

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

@Autowired
private UserDAO userDAO;

@Autowired
private ProductDAO productDAO;

@GetMapping(&quot;/users&quot;)
public List&lt;User&gt; showUsers(){
	
	return userDAO.showUsers();
}

@GetMapping(&quot;/products&quot;)
public List&lt;Product&gt; 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=&quot;UserId&quot;)
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:

确定