#30 jpa basic: jpql basic
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.hello.jpa.query.entity;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Address {
|
||||
|
||||
private String city;
|
||||
private String street;
|
||||
private String zipcode;
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getZipcode() {
|
||||
return zipcode;
|
||||
}
|
||||
|
||||
public void setZipcode(String zipcode) {
|
||||
this.zipcode = zipcode;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
package com.hello.jpa.query;
|
||||
package com.hello.jpa.query.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Member {
|
||||
@@ -12,6 +9,19 @@ public class Member {
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "team_id")
|
||||
private Team team;
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.hello.jpa.query.entity;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "orders")
|
||||
public class Order {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private int orderAmount;
|
||||
|
||||
@Embedded
|
||||
private Address address;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "product_id")
|
||||
private Product product;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getOrderAmount() {
|
||||
return orderAmount;
|
||||
}
|
||||
|
||||
public void setOrderAmount(int orderAmount) {
|
||||
this.orderAmount = orderAmount;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Product getProduct() {
|
||||
return product;
|
||||
}
|
||||
|
||||
public void setProduct(Product product) {
|
||||
this.product = product;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.hello.jpa.query.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Product {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String name;
|
||||
private int price;
|
||||
private int stockAmount;
|
||||
|
||||
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 int getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(int price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getStockAmount() {
|
||||
return stockAmount;
|
||||
}
|
||||
|
||||
public void setStockAmount(int stockAmount) {
|
||||
this.stockAmount = stockAmount;
|
||||
}
|
||||
}
|
||||
36
hello-jpa/src/main/java/com/hello/jpa/query/entity/Team.java
Normal file
36
hello-jpa/src/main/java/com/hello/jpa/query/entity/Team.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.hello.jpa.query.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Team {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "team")
|
||||
private List<Member> members = new ArrayList<>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.hello.jpa.query;
|
||||
|
||||
import com.hello.jpa.query.entity.Member;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.EntityTransaction;
|
||||
@@ -9,7 +11,7 @@ import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
import java.util.List;
|
||||
|
||||
public class JpaMain {
|
||||
public class jpqlBasic {
|
||||
|
||||
public static void main(String[] args) {
|
||||
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
|
||||
38
hello-jpa/src/main/java/com/hello/jpa/query/jpqlBasic2.java
Normal file
38
hello-jpa/src/main/java/com/hello/jpa/query/jpqlBasic2.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.hello.jpa.query;
|
||||
|
||||
import com.hello.jpa.query.entity.Member;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
public class jpqlBasic2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
|
||||
|
||||
EntityManager em = emf.createEntityManager();
|
||||
|
||||
EntityTransaction tx = em.getTransaction();
|
||||
tx.begin();
|
||||
|
||||
try {
|
||||
Member member = new Member();
|
||||
member.setName("member");
|
||||
member.setAge(10);
|
||||
em.persist(member);
|
||||
|
||||
Member singleResult = em.createQuery("select m from Member m where m.name = :name", Member.class)
|
||||
.setParameter("name", "member")
|
||||
.getSingleResult();
|
||||
|
||||
System.out.println("singleResult = " + singleResult);
|
||||
|
||||
tx.commit();
|
||||
} catch (Exception e) {
|
||||
tx.rollback();
|
||||
} finally {
|
||||
em.close();
|
||||
}
|
||||
|
||||
emf.close();
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,10 @@
|
||||
<!-- <class>com.hello.jpa.valuetype.ex00.Member</class>-->
|
||||
<!-- <class>com.hello.jpa.valuetype.ex01.Member</class>-->
|
||||
<!-- <class>com.hello.jpa.valuetype.ex02.Member</class>-->
|
||||
<class>com.hello.jpa.query.Member</class>
|
||||
<class>com.hello.jpa.query.entity.Member</class>
|
||||
<class>com.hello.jpa.query.entity.Order</class>
|
||||
<class>com.hello.jpa.query.entity.Product</class>
|
||||
<class>com.hello.jpa.query.entity.Team</class>
|
||||
|
||||
<properties>
|
||||
<!-- 필수 속성 -->
|
||||
|
||||
Reference in New Issue
Block a user