java - Understanding Single Table Inheritance in JPA -


I am new to JPA and am doing a small sample to know about it. But I have a problem below, please help me, and please explain why:

I have a class customer. Java, which is mapped to table client in DB:

  @ Entity Public Class Customer Device Serializable {Private Static Finals Long Serial Weerission UID = 1 L; @ Id @ column (name = "id_customer") @ Generated Values ​​(strategy = generation type. INNNTTY) Private Long ID; // Account Number Field Map Private string loginId with accountNumber column in the account table @ column (name = "loginId", unique = true); @column (name = "password") private string password; @column (name = "firstname") private string first name; @ Column (name = "last name") personal string lastName; @ Column (name = "address") private string address; @column (name = "email") private string email; @column (name = "phone") personal string phone; @OneToMany (mapped = "customer") private list & lt; Account & gt; AccountList; @OneToMany (mapped = "customer") private list & lt; Cards & gt; CardList; // Gates and Sets here.}  

In the above category there are two lists, account lists and cardlist, their generic class (card and account) have used Base Infos as a single table inheritance.

My BaseInfo.java:

  @ Entity @Table (name = "account") @ Inheritance (strategy = inheritance type SINGLE_TABLE) @DiscriminatorColumn (name = "discriminator" , DiscriminatorType = DiscriminatorType.STRING) Public class baseInfo implementation serializable {Private Static Final Pending Serial version UID = 1 L; @ Id @ column (name = "number") private string number; @column (name = "available number") private long available numbers; // getter and setter here}  

class card.Java:

  @ttelet (name = "account") @inherits (strategy = inheritance Type). SINGLE_TABLE) @DiscriminatorValue (value = "C") applies the public class card baseInfo tool Serializable {Personal Static last long serial VERSIONUID = 1L; @colome (name = "card type") type private string card; @ManyToOne @JoinColumn (name = "id_customer") Private customer client; // getter and setter}  

and square account.java:

 @ code> @table (name = "account") @inherities (strategy = inheritance Type) .SINGLE_TABLE) @DiscriminatorValue (value = "A") applies the BaseInfo tool to a public class account, Serializable {Private Stable, last long serial VERSIONUID = 1L; @column (name = "accountName") private string accountName; @column (name = "accounttip") private string account type; @ Temporal (temoor type. TIMESTAMP) @column (name = "dt_credited") private dead date date; @ Temporal (TemporalType.TIMESTAMP) @column (name = "dt_lst_updt") Private date Last updated date; @ManyToOne @JoinColumn (name = "id_customer") Private customer client; // getter, setter}  

Again, I do a query that queries the customer with loginid and password, such as:

  entityTransaction .begin (); TypedQuery & LT; Customers & gt; Query = entityManager.createQuery ("Select cfr" + client.clash.getName () + "C-where c.loginId =: loginId", Customer.class); Query.setParameter ("login id", loginId); Res = query.getSingleResult (); EntityTransaction.commit (); The code is run without any errors, but the result is strange to me: When I debug (or print the result on jsp), the account list or cardlist contains all the account of that customer, as they are 'discriminatory 'Do not care about the column.  

I have 2 questions:

  1. How can I collect that target only card in the list card (discrimination = C) and in the list Account is account only (discriminant = A)?

  2. Is there an alternative way to select a query? First account without querying the card or list client (like I use) ??

    Thank you in advance! : D

    I'm not sure whether this is a JPA restriction or hibernation-specific restriction, but You can not use the same columns to map to two different organizations.

    To map the relationship between the customer and the card, you should use something like car_customer_id , and account_customer_id the relationship between the customer and the accounts

    to map

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -