Many To One JPA Example

Here is my example!

Table Structure
mysql> DESCRIBE city;
+-------------+----------------------+------+-----+
| FIELD       | TYPE                 | NULL | KEY |
+-------------+----------------------+------+-----+
| city_id     | SMALLINT(5) UNSIGNED | NO   | PRI |
| city        | VARCHAR(50)          | NO   |     |
| country_id  | SMALLINT(5) UNSIGNED | NO   | MUL |
| last_update | TIMESTAMP            | NO   |     |
+-------------+----------------------+------+-----+
 
mysql> DESCRIBE country;
+-------------+----------------------+------+-----+
| FIELD       | TYPE                 | NULL | KEY |
+-------------+----------------------+------+-----+
| country_id  | SMALLINT(5) UNSIGNED | NO   | PRI |
| country     | VARCHAR(50)          | NO   |     |
| last_update | TIMESTAMP            | NO   |     |
+-------------+----------------------+------+-----+

*.java
package biz.tugay.saqila.model;
/* User: koray@tugay.biz Date: 07/08/15 Time: 15:01 */
 
import javax.persistence.*;
 
@Entity
@Table(name = "city")
public class City {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int city_id;
 
    @Basic
    private String city;
 
    @ManyToOne(targetEntity = Country.class)
    @JoinColumn(name = "country_id")
    private Country country;
 
    // Getters, Setters
}
 
package biz.tugay.saqila.model;
/* User: koray@tugay.biz Date: 07/08/15 Time: 15:02 */
 
import javax.persistence.*;
import java.util.List;
 
@Entity
@Table(name = "country")
public class Country {
 
    @Id
    @GeneratedValue( strategy=GenerationType.AUTO )
    private int country_id;
 
    @Basic
    private String country;
 
    @OneToMany(mappedBy = "country")
    private List<City> cityList;
 
    // Getters, Setters
}