I am new to JPA and doing a small sample to learn about it. But I got one problem below, please help me out, and please explain why:
I have class Customer.java, which is mapped to table customer in db:
@Entity
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id_customer")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
// accountNumber field maps with accountNumber column in Account table
@Column(name = "loginId", unique = true)
private String loginId;
@Column(name = "password")
private String password;
@Column(name = "firstName")
private String firstName;
@Column(name = "lastName")
private String lastName;
@Column(name = "address")
private String address;
@Column(name = "email")
private String email;
@Column(name = "phone")
private String phone;
@OneToMany(mappedBy="customer")
private List<Account> accountList;
@OneToMany(mappedBy="customer")
private List<Card> cardList;
// getters and setters goes here
}
The above class has two lists, accountList and cardList, their generic Class (Card and Account) extends BaseInfo using Single table Inheritance.
Here is my BaseInfo.java:
@Entity
@Table(name = "account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public class BaseInfo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "number")
private String number;
@Column(name = "availableNumber")
private Long availableNumber;
//getter and setter here
}
Class Card.java:
@Entity
@Table(name = "account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value = "C")
public class Card extends BaseInfo implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "cardType")
private String cardType;
@ManyToOne
@JoinColumn(name = "id_customer")
private Customer customer;
//getter and setter
}
And class Account.java:
@Entity
@Table(name = "account")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value = "A")
public class Account extends BaseInfo implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "accountName")
private String accountName;
@Column(name = "accountType")
private String accountType;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "dt_created")
private Date createdDate;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "dt_lst_updt")
private Date lastUpdatedDate;
@ManyToOne
@JoinColumn(name = "id_customer")
private Customer customer;
//getter, setter
}
Then, I do a query that query customer from database with loginid and password, like this:
entityTransaction.begin();
TypedQuery<Customer> query = entityManager.createQuery(
"SELECT c FROM " + Customer.class.getName()
+ " c Where c.loginId= :loginId", Customer.class);
query.setParameter("loginId", loginId);
res = query.getSingleResult();
entityTransaction.commit();
The code run with no error, but the result is somethings strange to me: When I debug (or print out the result to jsp), accountList or cardList contains all Account of that customer, just like they don't care about the 'discriminator' column.
I have 2 questions:
How can I archive the goal that listCard contains only Card (discrimination = c) and listAccount contains only Account (discriminator = a) ?
Is there an alternative way to query listCard or listAccount without query the customer first (like I use) ??
Thank in advance! :D