Suppose i have a classes like this
@Entity
public class Customer implements Serializable {
private int id;
private String firstName;
private String lastName;
private Referee referee;
private Collection<Address> addresses;
private Collection<Account> accounts;
.....
} //end of class Customer
@Entity
public class Referee implements Serializable {
private int id;
private String name;
private String comments;
....
} //end of class Referee
@Entity
public class Account implements Serializable {
private int id;
private double balance;
private String accountType;
...
}
@Entity
public class Address implements Serializable {
private int id;
private String addressLine;
private String country;
private String postCode;
...
}
Now if i make Table in database of name Customer. Then what type will i use for the type Referee, Collection<Address> and Collection<Account>. Like if i make column id, then it’s type will be INTEGER, String changes to VARCHAR but what type do i use for Collection and Referee?
Thanks
The referee field would be integer in the database because it has to store the id field of the Referee class which is of type int.
Collections will also be based on the id’s of classes. For 1 to N relationships a column on the N side can be used. So if a Customer has multiple addresses but an address never belongs to more then one Customer then you can store the id of the customer in the address table to store the relationship. If it is N to M you will need an additional so called join table which stores paires of customer id + address id. Same story for accounts.
BTW many JPA providers can generate the tables for you see their manual for how.
If you are new to JPA you should get a good book about it. A couple of tutorials will not give you enough in depth knowledge to use it well. Personally I have read Pro EJB 3: Java Persistence API.
When learning JPA the netbeans IDE can be a great help as well as it will give you lots of useful hints for annotating your entities. It will for instance notice that your collections need additional annotations and will give you choices for one to many or many to many and uni- or bidirectional.