Spring

Spring Boot에서 내부 클래스를 열 필드 변수로 사용하고 매핑하는 방법은 무엇입니까?

기록만이살길 2021. 3. 8. 15:05
반응형

Spring Boot에서 내부 클래스를 열 필드 변수로 사용하고 매핑하는 방법은 무엇입니까?

1. 질문(문제점):

열 유형 중 하나가 너무 복잡하다는 오류가 표시됩니다 (List<Foo>). 내부 클래스를 열 유형으로 올바르게 사용할 수있는 방법은 무엇입니까?

다음은 내 모델 코드의 관련 스 니펫입니다.

Users.java

@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(name = "name")
private String name;

@Column(name = "password")
private String password;

@Column(name = "todos")
private List<TodoItem> todos = new ArrayList<TodoItem>();

public static class TodoItem {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected long id;
    protected String todo;
    protected boolean completed;

    public TodoItem() {}

    public TodoItem(String todo, boolean completed) {
        // super();
        this.todo = todo;
        this.completed = completed;
    }
}

public User(String name, String password, List<TodoItem> todos) {
    super();
    this.name = name;
    this.password = password;
    this.todos = todos;
}

그리고 여기에 내가 얻는 관련 오류 출력이 있습니다.

콘솔 로그

2021-01-15 01:18:30.966 ERROR 1480 --- [           main] j.LocalContainerEntityManagerFactoryBean : 
Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate 
SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: 
java.util.List, at table: users, for columns: [org.hibernate.mapping.Column(todos)]
2021-01-15 01:18:30.967  WARN 1480 --- [           main] ConfigServletWebServerApplicationContext : 
Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'entityManagerFactory' defined in class path resource 
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init 
method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] 
Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could 
not determine type for: java.util.List, at table: users, for columns: 
[org.hibernate.mapping.Column(todos)]
Error starting ApplicationContext. To display the conditions report re-run your application with 
'debug' enabled.
2021-01-15 01:18:31.004 ERROR 1480 --- [           main] o.s.boot.SpringApplication               : 
Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'entityManagerFactory' defined in class path resource 
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init 
method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] 
Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could 
not determine type for: java.util.List, at table: users, for columns: 
[org.hibernate.mapping.Column(todos)]

불가능할 거라고 생각하기 시작했고 내부 클래스를 별도의 테이블로 만들어야합니다.

2. 해결방안:

시험

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "name")
    private String name;

    @Column(name = "password")
    private String password;

    @OneToMany
    @JoinColumn(name = "user_id")
    private List<TodoItem> todos = new ArrayList<>();

    public User() {}

    public User(String name, String password, List<TodoItem> todos) {
        this.name = name;
        this.password = password;
        this.todos = todos;
    }

    @Entity
    @Table(name = "todo_item")
    public static class TodoItem {

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        protected long id;

        @Column(name = "todo")
        protected String todo;
        
        @Column(name = "completed")
        protected boolean completed;

        public TodoItem() {
        }

        public TodoItem(String todo, boolean completed) {
            this.todo = todo;
            this.completed = completed;
        }

        // getter/setter ..
    }

    // getter/setter ..
}
65733454
반응형