해결된 질문
작성
·
1.1K
·
수정됨
0
[질문 내용]
maven이 아니라 gradle로 시도를 해보고 싶어서
스프링부트로 생성하여 진행을 해봤는데
em.persist(member);를 실행할때 에러가 발생하는데
도저히 원인을 못찾겠는데 혹시 어떤 문제인지 아시나요?
package com.example.demo.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Member {
@Id
private Long id;
private String name;
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;
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.demo.entity.Member;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.Persistence;
@SpringBootApplication
public class StudySpringJpaApplication {
public static void main(String[] args) {
SpringApplication.run(StudySpringJpaApplication.class, args);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello"); //애플리케이션 전체 공유 (persistence.xml 참조)
EntityManager em = emf.createEntityManager(); //한번 쓰고 버려야함, 쓰레드간 공유하지 않음
EntityTransaction tx = em.getTransaction(); //조회를 제외한 DML 작업시 필수로 사용
tx.begin();
try {
Member member = new Member();
member.setId(1L);
member.setName("memberA");
em.persist(member);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
System.out.println("=== ROLLBACK 진행 ===");
} finally {
em.close();
}
emf.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<!-- EntityManagerFactory 생성 시 사용되는 persistence name -->
<persistence-unit name="hello">
<class>com.example.demo.entity.Member</class>
<properties>
<!-- 필수 속성 -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="비밀번호"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/jpa?characterEncoding=UTF-8&serverTimezone=UTC"/>
<!-- 하이버네이트 사용 시 다른 DB에서 MySQL 문법을 사용 가능하도록 변경.-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect"/>
<!-- 콘솔에 SQL 출력 여부 -->
<property name="hibernate.show_sql" value="true"/>
<!-- 가독성 높여주는 formatting 여부 -->
<property name="hibernate.format_sql" value="true"/>
<!-- Comment 확인 여부 -->
<property name="hibernate.use_sql_comments" value="true"/>
</properties>
</persistence-unit>
</persistence>
에러는 다음과 같이 발생합니다.
jakarta.persistence.PersistenceException: Converting `org.hibernate.property.access.spi.PropertyAccessException` to JPA `PersistenceException` : Error accessing field [private java.lang.Long com.example.demo.entity.Member.id] by reflection for persistent property [com.example.demo.entity.Member#id] : com.example.demo.entity.Member@1bc6c7f0
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:165)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:175)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:182)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:743)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:721)
at com.example.demo.StudySpringJpaApplication.main(StudySpringJpaApplication.java:29)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Long com.example.demo.entity.Member.id] by reflection for persistent property [com.example.demo.entity.Member#id] : com.example.demo.entity.Member@1bc6c7f0
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:48)
at org.hibernate.metamodel.mapping.internal.BasicEntityIdentifierMappingImpl.getIdentifier(BasicEntityIdentifierMappingImpl.java:148)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:5269)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4856)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:293)
at org.hibernate.event.internal.EntityState.getEntityState(EntityState.java:59)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:93)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:53)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:737)
... 7 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field com.example.demo.entity.Member.id to com.example.demo.entity.Member
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.base/java.lang.reflect.Field.get(Field.java:425)
at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:44)
... 16 more
=== ROLLBACK 진행 ===
spring-boot-devtools로 인한 클래스로더 이슈였습니다.
답변 감사합니다.