인프런 커뮤니티 질문&답변

IamZero-j님의 프로필 이미지
IamZero-j

작성한 질문수

자바 ORM 표준 JPA 프로그래밍 - 기본편

객체지향 쿼리 언어1 -기본문법 : 프로덕션 오류가 납니다 ㅠ

작성

·

1.4K

0

안녕하세요 강사님. 이번 수업을 듣다가 오류가 나서 질문드려요

강의와 관련있는 질문을 남겨주세요.

org.hibernate.hql.internal.ast.ErrorTracker reportError

ERROR: <AST>:1:36: unexpected AST node: .

우선 JpaMain 클래스 입니다. 

public class JpaMain {
public static void main(String[] args){
EntityManagerFactory emf= Persistence.createEntityManagerFactory("hello"); //PersistenceUnitName

EntityManager em=emf.createEntityManager();
EntityTransaction tx=em.getTransaction();
tx.begin();

try{
Member member=new Member();
member.setUsername("member1");
member.setAge(10);
em.persist(member);

em.flush();
em.clear();

List<Member> result=em.createQuery("Select m from Member m where m.username",Member.class).getResultList(); //엔티티 프로젝션
Member findMember=result.get(0);
findMember.setAge(20);

//List<Team> result=em.createQuery("Select m.team from Member m join m.team",Team.class).getResultList(); //엔티티프로덕션
//em.createQuery("Select o.address from Order o",Address.class).getResultList(); //임베디드 타입 프로덕션

//List resultList = em.createQuery("Select distinct m.username, m.age from Member m").getResultList(); //스칼라 타입 프로젝션

//Object o=resultList.get(0);
//Object[] result=(Object[]) o;

//System.out.println("username = "+result[0]);
//System.out.println("age = "+result[1]);
tx.commit();
}
catch(Exception e){
tx.rollback();
}
finally {
em.close();
}
emf.close();

}
}

엔티티 프로젝션을 실습하는데 오류가 나서 질문 드립니다.

엔티티 프로덕션, 임베디드 타입 프로젝션, 스칼라 타입 프로젝션 은 잘 작동 되는데 엔티티 프로젝션은 오류가 납니다.

h2 데이터베이스에는 테이블만 생성될 뿐 데이터가 삽입되지 않았습니다.ㅠㅠ...



[인텔리제이 컴파일 결과 오류]

"C:\Program Files\Java\jdk-11.0.8\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\lib\idea_rt.jar=65515:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\IamZero\Desktop\spring\JPQL\target\classes;C:\Users\IamZero\.m2\repository\org\hibernate\hibernate-entitymanager\5.4.22.Final\hibernate-entitymanager-5.4.22.Final.jar;C:\Users\IamZero\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\IamZero\.m2\repository\org\hibernate\hibernate-core\5.4.22.Final\hibernate-core-5.4.22.Final.jar;C:\Users\IamZero\.m2\repository\org\javassist\javassist\3.24.0-GA\javassist-3.24.0-GA.jar;C:\Users\IamZero\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\IamZero\.m2\repository\org\jboss\jandex\2.1.3.Final\jandex-2.1.3.Final.jar;C:\Users\IamZero\.m2\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;C:\Users\IamZero\.m2\repository\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar;C:\Users\IamZero\.m2\repository\org\glassfish\jaxb\jaxb-runtime\2.3.1\jaxb-runtime-2.3.1.jar;C:\Users\IamZero\.m2\repository\org\glassfish\jaxb\txw2\2.3.1\txw2-2.3.1.jar;C:\Users\IamZero\.m2\repository\com\sun\istack\istack-commons-runtime\3.0.7\istack-commons-runtime-3.0.7.jar;C:\Users\IamZero\.m2\repository\org\jvnet\staxex\stax-ex\1.8\stax-ex-1.8.jar;C:\Users\IamZero\.m2\repository\com\sun\xml\fastinfoset\FastInfoset\1.2.15\FastInfoset-1.2.15.jar;C:\Users\IamZero\.m2\repository\org\dom4j\dom4j\2.1.3\dom4j-2.1.3.jar;C:\Users\IamZero\.m2\repository\org\hibernate\common\hibernate-commons-annotations\5.1.0.Final\hibernate-commons-annotations-5.1.0.Final.jar;C:\Users\IamZero\.m2\repository\javax\persistence\javax.persistence-api\2.2\javax.persistence-api-2.2.jar;C:\Users\IamZero\.m2\repository\net\bytebuddy\byte-buddy\1.10.10\byte-buddy-1.10.10.jar;C:\Users\IamZero\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.1.1.Final\jboss-transaction-api_1.2_spec-1.1.1.Final.jar;C:\Users\IamZero\.m2\repository\com\h2database\h2\1.4.200\h2-1.4.200.jar;C:\Users\IamZero\.m2\repository\javax\xml\bind\jaxb-api\2.3.0\jaxb-api-2.3.0.jar jpql.JpaMain 3월 07, 2021 12:05:47 오전 org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation INFO: HHH000204: Processing PersistenceUnitInfo [name: hello] 3월 07, 2021 12:05:47 오전 org.hibernate.Version logVersion INFO: HHH000412: Hibernate ORM core version 5.4.22.Final 3월 07, 2021 12:05:48 오전 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final} 3월 07, 2021 12:05:48 오전 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) 3월 07, 2021 12:05:48 오전 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:tcp://localhost/~/test] 3월 07, 2021 12:05:48 오전 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {password=****, user=sa} 3월 07, 2021 12:05:48 오전 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false 3월 07, 2021 12:05:48 오전 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 3월 07, 2021 12:05:48 오전 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect Hibernate: drop table if exists Member CASCADE Hibernate: drop table if exists ORDERS CASCADE Hibernate: drop table if exists Product CASCADE Hibernate: drop table if exists Team CASCADE Hibernate: drop sequence if exists hibernate_sequence Hibernate: create sequence hibernate_sequence start with 1 increment by 1 Hibernate: create table Member ( id bigint not null, age integer not null, username varchar(255), TEAM_ID bigint, primary key (id) ) Hibernate: create table ORDERS ( id bigint not null, city varchar(255), street varchar(255), zipcode varchar(255), orderAmount integer not null, PRODUCT_ID bigint, primary key (id) ) Hibernate: create table Product ( id bigint not null, name varchar(255), price integer not null, stockAmount integer not null, primary key (id) ) Hibernate: create table Team ( id bigint not null, name varchar(255), primary key (id) ) Hibernate: alter table Member add constraint FKl7wsny760hjy6x19kqnduasbm foreign key (TEAM_ID) references Team Hibernate: alter table ORDERS add constraint FKtlx3qxs8vwir2b80i3oumx2qm foreign key (PRODUCT_ID) references Product 3월 07, 2021 12:05:49 오전 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@77b919a3] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. 3월 07, 2021 12:05:49 오전 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@1cd3b138] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. 3월 07, 2021 12:05:49 오전 org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] Hibernate: call next value for hibernate_sequence Hibernate: /* insert jpql.Member */ insert into Member (age, TEAM_ID, username, id) values (?, ?, ?, ?) 3월 07, 2021 12:05:49 오전 org.hibernate.hql.internal.ast.ErrorTracker reportError ERROR: <AST>:1:36: unexpected AST node: . 3월 07, 2021 12:05:49 오전 org.hibernate.hql.internal.ast.ErrorTracker reportError ERROR: <AST>:1:36: unexpected AST node: . <AST>:1:36: unexpected AST node: . at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2169) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:827) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:621) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:325) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:273) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:162) at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:604) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:716) at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:816) at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23) at jpql.JpaMain.main(JpaMain.java:23) 3월 07, 2021 12:05:49 오전 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState stop INFO: HHH10001008: Cleaning up connection pool [jdbc:h2:tcp://localhost/~/test] Process finished with exit code 0


-------------------------------------------------------------------------------------------------------------------------------


스칼라 타입 프로젝션 실습의 경우 데이터도 잘 나오고 있습니다.

List resultList = em.createQuery("Select distinct m.username, m.age from Member m").getResultList(); //스칼라 타입 프로젝션

Object o=resultList.get(0);
Object[] result=(Object[]) o;



-----------------------------------------------------------------------------------------------------------------------------
다음은 실습하는데 사용한 파일입니다

[Address]
@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;
}
}

[Member]
@Entity
public class Member {
@Id @GeneratedValue
private Long id;
private String username;
private int age;

@ManyToOne
@JoinColumn(name="TEAM_ID")
private Team team;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Team getTeam() {
return team;
}

public void setTeam(Team team) {
this.team = team;
}
}

[Order]
@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;
}
}

[Product]
@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;
}
}

[Team]
@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;
}


public List<Member> getMembers() {
return members;
}

public void setMembers(List<Member> members) {
this.members = members;
}
}

답변 2

0

김영한님의 프로필 이미지
김영한
지식공유자

안녕하세요. IamZero-j님

현재 쿼리를 보면 where m.username 다음에 파라미터 조건이 없습니다.

Select m from Member m where m.username

다음과 같이 작성해주세요.

Select m from Member m where m.username = :username

감사합니다.

0

김영한님의 프로필 이미지
김영한
지식공유자

안녕하세요. IamZero-j님

 

전체 프로젝트를 압축해서 올려주세요.

감사합니다.

 

IamZero-j님의 프로필 이미지
IamZero-j

작성한 질문수

질문하기