英文:
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;
}
请注意,在这些代码中,我用双引号 ""
替换了原始代码中的 "
。这将使代码更易读且易于编译。如果你需要进一步的帮助,请随时告诉我。
英文:
With the query Select c from Card c join User u on c.userId=u.userId where u.username=':u'
, 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="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;
答案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 = "UserId", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论