» JPA

Creating a simple MySql Table:

CREATE TABLE `YairShinar`.`employee` (
`idemployee` INT NOT NULL ,
`firstname` VARCHAR(45) NULL ,
`lastname` VARCHAR(45) NULL ,
PRIMARY KEY (`idemployee`) );

Creating the JPA annotations and imports

package com.YairShinar.jpa.entities.Employee;

import java.io.Serializable;
import javax.persistence.*;

@Entity
public class Employee implements Serializable {

@Id
private int idEmployee;

private String firstname;

private String lastname;

public Employee() {
}

public int getIdEmployee() {
return this.idEmployee;
}

public void setIdEmployee(int idEmployee) {
this.idEmployee = idEmployee;
}

public String getFirstname() {
return this.firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return this.lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

@Override
public String toString() {
return "Employee [idEmployee=" + idEmployee ", firstname=" + firstname + ", lastname=" + lastname + "]";
}

}

Edit the persistence.xml file

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.1"

xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

<persistence-unit name="JPAExamples">

<class>com.YairShinar.jpa.entities.Employee</class>

<properties>

<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/YairShinar" />

<property name="javax.persistence.jdbc.user" value="yourUserNameForDB" />

<property name="javax.persistence.jdbc.password" value="yourPasswordForDB" />

</properties>

</persistence-unit>

</persistence>