引用 JPQL 中的连接列

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

Referencing join column in JPQL

问题

使用查询语句 Select c from Card c join User u on c.userId=u.userId where u.username=:u,我试图根据User表中的用户名来获取用户的卡片列表。然而,在运行时,我遇到了antlr.SemanticException: could not resolve property: userId of: com.abc.SheridanSportsWAR.entity.Card异常。我该如何引用我的Card实体中的 UserId 列?

Card.java

@Entity
public class Card {
	
	@Id
	@Column(name="CardId")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer cardId;
	
	@Column(name="CardNumber")
	private String cardNumber;
	
	@Column(name="CardType")
	private String cardType;
	
	@Column(name="ExpiryMonth")
	private String expiryMonth;
	
	@Column(name="ExpiryYear")
	private String expiryYear;
	
	@Column(name="CardHolder")
	private String cardHolder;

	@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH})
	@JoinColumn(name = "UserId")
	private User userJ;
	
	@OneToMany(mappedBy = "cardJ")
	private List<Purchase> purchases;
}

User.java

@Entity
public class User {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="UserId")
	private Integer userId;
	
	@Column(name="FirstName")
	private String firstName;
	
	@Column(name="LastName")
	private String lastName;
	
	@Column(name="Email")
	private String email;
	
	@Column(name="Username")
	private String username;
	
	@Column(name="Password")
	private String password;
	
	@Column(name="RoleName")
	private String roleName;
	
	@OneToMany(mappedBy="userJ", cascade= CascadeType.ALL)
	private List<Card> cards;
}

请注意,在这些代码中,我用双引号 "" 替换了原始代码中的 &quot;。这将使代码更易读且易于编译。如果你需要进一步的帮助,请随时告诉我。

英文:

With the query Select c from Card c join User u on c.userId=u.userId where u.username=&#39;:u&#39;, I am trying to grab a list of Cards for the User based on the username available in User the table. However, I am running into the antlr.SemanticException: could not resolve property: userId of: com.abc.SheridanSportsWAR.entity.Card exception when I run it. How do I reference the UserId column in my Card entity?

Card.java

@Entity
public class Card {
	
	@Id
	@Column(name=&quot;CardId&quot;)
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer cardId;
	
	@Column(name=&quot;CardNumber&quot;)
	private String cardNumber;
	
	@Column(name=&quot;CardType&quot;)
	private String cardType;
	
	@Column(name=&quot;ExpiryMonth&quot;)
	private String expiryMonth;
	
	@Column(name=&quot;ExpiryYear&quot;)
	private String expiryYear;
	
	@Column(name=&quot;CardHolder&quot;)
	private String cardHolder;

	@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH})
	@JoinColumn(name = &quot;UserId&quot;)
	private User userJ;
	
	@OneToMany(mappedBy = &quot;cardJ&quot;)
	private List&lt;Purchase&gt; purchases;

User.java

@Entity
public class User {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name=&quot;UserId&quot;)
	private Integer userId;
	
	@Column(name=&quot;FirstName&quot;)
	private String firstName;
	
	@Column(name=&quot;LastName&quot;)
	private String lastName;
	
	@Column(name=&quot;Email&quot;)
	private String email;
	
	@Column(name=&quot;Username&quot;)
	private String username;
	
	@Column(name=&quot;Password&quot;)
	private String password;
	
	@Column(name=&quot;RoleName&quot;)
	private String roleName;
	
	@OneToMany(mappedBy=&quot;userJ&quot;, cascade= CascadeType.ALL)
	private List&lt;Card&gt; cards;

答案1

得分: 2

错误消息非常清楚。在您的 Card 实体中没有 userId 字段。像这样创建一个:

@Column(name = "UserId", insertable=false, updatable=false)
private Integer userId;
英文:

The error message is pretty clear. There is no field userId in your Card entity. Create one like that:

@Column(name = &quot;UserId&quot;, insertable=false, updatable=false)
private Integer userId;

答案2

得分: 0

据我所知,问题似乎是在“card”表中没有“userId”列。

英文:

AFAIK, the problem seems to be that there's no userId column in the card table.

huangapple
  • 本文由 发表于 2020年9月1日 17:36:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63685108.html
匿名

发表评论

匿名网友

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

确定