Java Tutorial/JPA/Update

Материал из Java эксперт
Перейти к: навигация, поиск

EJB QL Update With Condition

File: Main.java



import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
  static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
  static EntityManager em = emf.createEntityManager();
  public static void main(String[] a) throws Exception {
    em.getTransaction().begin();
    Query query = em.createQuery("UPDATE Student e SET e.salary = 60 WHERE e.salary = 55");
    query.executeUpdate();
    em.getTransaction().rumit();
    em.close();
    emf.close();
    Helper.checkData();
  }
}





Update an Entity from find method

File: Gender.java



public enum Gender {
  MALE, FEMALE
}





Use Entity Returned From Merge Operation

File: Student.java



import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Student {
    @Id
    private int id;
    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;
    }

    public String toString() {
        return "Student id: " + getId() + " name: " + getName();
    }
}