JPA - @Version Examples

Code Snippets JPA JAVA EE 

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Version;

@Entity
public class Employee {
@Id
@GeneratedValue
private Integer id;
@Version
private long version;
private String name;
private String department;

public Employee() {
}

public Employee(String name, String department) {
this.name = name;
this.department = department;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}


public Integer getId() {
return id;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", version=" + version +
", name='" + name + '\'' +
", department='" + department + '\'' +
'}';
}
}
Original Post




@Entity
public class Phone {
@Id
@GeneratedValue
private int id;
@Version
private long version;
private String number;
private String type;

public Phone() {
}

public Phone(String number, String type) {
this.number = number;
this.type = type;
}

public static Phone cell(String number) {
return new Phone(number, "cell");
}

public static Phone work(String number) {
return new Phone(number, "work");
}

public static Phone home(String number) {
return new Phone(number, "home");
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}


@Override
public String toString() {
return "Phone{" +
"id=" + id +
", version=" + version +
", number='" + number + '\'' +
", type='" + type + '\'' +
'}';
}
}
Original Post




See Also