Merge branch 'master' into master
This commit is contained in:
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.jpa.basicannotation;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Course {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
@Basic(optional = false, fetch = FetchType.LAZY)
|
||||
private String name;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.jpa.defaultvalues;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@Column(columnDefinition = "varchar(255) default 'John Snow'")
|
||||
private String name = "John Snow";
|
||||
|
||||
@Column(columnDefinition = "integer default 25")
|
||||
private Integer age = 25;
|
||||
|
||||
@Column(columnDefinition = "boolean default false")
|
||||
private Boolean locked = false;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Boolean getLocked() {
|
||||
return locked;
|
||||
}
|
||||
|
||||
public void setLocked(Boolean locked) {
|
||||
this.locked = locked;
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.jpa.defaultvalues;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
public class UserRepository {
|
||||
|
||||
private EntityManagerFactory emf = null;
|
||||
|
||||
public UserRepository() {
|
||||
emf = Persistence.createEntityManagerFactory("entity-default-values");
|
||||
}
|
||||
|
||||
public User find(Long id) {
|
||||
EntityManager entityManager = emf.createEntityManager();
|
||||
User user = entityManager.find(User.class, id);
|
||||
entityManager.close();
|
||||
return user;
|
||||
}
|
||||
|
||||
public void save(User user, Long id) {
|
||||
user.setId(id);
|
||||
|
||||
EntityManager entityManager = emf.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.persist(user);
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
emf.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Article {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String title;
|
||||
|
||||
@Enumerated(EnumType.ORDINAL)
|
||||
private Status status;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Type type;
|
||||
|
||||
@Basic
|
||||
private int priorityValue;
|
||||
|
||||
@Transient
|
||||
private Priority priority;
|
||||
|
||||
private Category category;
|
||||
|
||||
public Article() {
|
||||
}
|
||||
|
||||
@PostLoad
|
||||
void fillTransient() {
|
||||
if (priorityValue > 0) {
|
||||
this.priority = Priority.of(priorityValue);
|
||||
}
|
||||
}
|
||||
|
||||
@PrePersist
|
||||
void fillPersistent() {
|
||||
if (priority != null) {
|
||||
this.priorityValue = priority.getPriority();
|
||||
}
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Status status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Priority getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
public void setPriority(Priority priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public Category getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(Category category) {
|
||||
this.category = category;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public enum Category {
|
||||
SPORT("S"), MUSIC("M"), TECHNOLOGY("T");
|
||||
|
||||
private String code;
|
||||
|
||||
Category(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Converter(autoApply = true)
|
||||
public class CategoryConverter implements AttributeConverter<Category, String> {
|
||||
@Override
|
||||
public String convertToDatabaseColumn(Category category) {
|
||||
if (category == null) {
|
||||
return null;
|
||||
}
|
||||
return category.getCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Category convertToEntityAttribute(final String code) {
|
||||
if (code == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Stream.of(Category.values())
|
||||
.filter(c -> c.getCode().equals(code))
|
||||
.findFirst()
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public enum Priority {
|
||||
LOW(100), MEDIUM(200), HIGH(300);
|
||||
|
||||
private int priority;
|
||||
|
||||
private Priority(int priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public int getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
public static Priority of(int priority) {
|
||||
return Stream.of(Priority.values())
|
||||
.filter(p -> p.getPriority() == priority)
|
||||
.findFirst()
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
public enum Status {
|
||||
OPEN, REVIEW, APPROVED, REJECTED;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
enum Type {
|
||||
INTERNAL, EXTERNAL;
|
||||
}
|
||||
Reference in New Issue
Block a user