Step 2: Define JPA Entity Classes
First we will create a new package named ‘bean’ to include entity classes.
Right click on City node or Source Packages node to get the popup menu and select New. The recent selections are listed and find the Java Package.
If it is not listed in popup menu, select Other to get the [New File] window. Select Java from Categorise and Java Package from File Types.
In the New Java Package window, enter bean as the package name and make sure Source Packages is selected as the location. The click Finish to create the package.
We can create Entity classes by creating a java class and adding @Entity annotation just above the class definition line. NetBeans also allow us to directly add an Entity Class.
To create a new entity class right click on bean and select New > Entity Class. If Entity Class is not listed under recent types, select Other to get the New File Window. Select Persistence under categories and Entity Class as the file type
In the New Entity Class window, type City as the Class Name. Make sure bean is selected as the package. By default, the Create Persistence Unit is selected and un-check it as it is not essential to have a persistence unit to work with ObjectDB. In this tutorial we do not use a persistent unit. Then click Finish to generate State Entity Class.
You will see that NetBeans has created most of the coding for us and we can edit the code to suit our needs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package bean;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Country implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Country)) {
return false;
}
Country other = (Country) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "bean.Country[id=" + id + "]";
}
} |
The class generated by NetBeans implements Serializable interface and it is not essential when working with ObjectDB. Therefore we can safely delete
- import java.io.Serializable (full line – no 3) ; and
- implements Serializable (in line no 10)
from the generated code.
The generated code has created an id field from Line 12 to 14. As we need a unique sequentially increasing id for the each newly created country object, we have to change
1 2 3 | @GeneratedValue(strategy = GenerationType.AUTO)
(to)
@GeneratedValue(strategy = GenerationType.IDENTITY). |
This will make sure when a new Country object is persisted, a unique sequential id specific to Country type will be automatically added.
We need to add a String field named ‘name’ as a property to the Country class. Add String name; to line no 15 of the above coding. It is needed to add getter and setter to the new property. Press Alt + Ins to get code insert code menu and select getter and setter
Then check [name : String] and click Generate to insert getters and setters.
We also have to modify the toString method to return the name of the country.
Finally the new coding of Country Entity will look like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Country {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Country)) {
return false;
}
Country other = (Country) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return name;
}
} |
The next step is to create a New Entity class named City. The very same above steps have to be followed and then add the following modifications.
Just below where the String name; is inserted, type Country country; This means every object made from City class has a reference to another persisting object from the Entity Country. Any single city has only one country. But a single country can have any number of cities. So there is one-to-many relationship. Just above the place we define Country country; in City Entity Class, we have to add the @ManyToOne annotation. As soon as we finish typing the Country country; NetBeans give us warning to add the annotation and all we have to select the correct one, [Create Unilateral ManyToOne Relationship]. Generate the getter and setter for the Country country; as described above.
Finally the new coding of City entity will look like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class City{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
String name;
@ManyToOne
Country country;
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof City)) {
return false;
}
City other = (City) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return name;
}
} |






February 19, 2011
Hibernate, JPA