You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IntelliJ中JPA创建对象后H2控制台无对应表生成问题求助

JPA生成数据库表失败排查(预期生成ITEM、MOVIE等表但H2控制台无对应结构)

问题核心

配置与代码存在两处关键问题,导致Hibernate无法正常生成DDL创建表结构:

  1. persistence.xml中hibernate.use_sql_comments属性值错误,该属性应为布尔类型,设置为10会触发配置加载异常,中断表生成流程。
  2. JpaMain中未执行实体持久化操作(em.persist()),虽hbm2ddl.auto=create理论上无需持久化也能生成表,但配置异常会导致该逻辑失效。

修复步骤

1. 修正persistence.xml配置错误

将错误的属性值改为布尔类型:

<!-- 原错误配置 -->
<property name="hibernate.use_sql_comments" value="10"/>
<!-- 修正后 -->
<property name="hibernate.use_sql_comments" value="true"/>

2. 显式指定JPA扫描的实体类(可选但推荐)

在persistence-unit中添加class标签,避免自动扫描失败:

<persistence-unit name="hello">
    <class>hellojpa.Item</class>
    <class>hellojpa.Movie</class>
    <class>hellojpa.Album</class>
    <class>hellojpa.Book</class>
    <class>hellojpa.Member</class>
    <class>hellojpa.Team</class>
    <class>hellojpa.Locker</class>
    <class>hellojpa.Product</class>
    <class>hellojpa.MemberProduct</class>
    <!-- 原有properties配置 -->
</persistence-unit>

3. 补充实体持久化操作

在JpaMain中添加em.persist(),确保实体被JPA处理,同时便于排查异常:

try{
    Movie movie = new Movie();
    movie.setDirector("aaaa");
    movie.setActor("bbbb");
    movie.setName("바람과 함께 사라지다");
    movie.setPrice(10000);
    em.persist(movie); // 添加此行
    tx.commit();
}catch(Exception e){
    tx.rollback();
    e.printStackTrace(); // 添加异常打印
} finally{
    em.close();
}

4. 验证H2连接一致性

确保H2控制台使用的URL与persistence.xml中一致:jdbc:h2:tcp://localhost/~/test,避免连接到错误数据库实例。


用户提供的实体类及配置文件

Album.java

package hellojpa;

import javax.persistence.Entity;

@Entity
public class Album extends Item{

    private String artist;
}

Book.java

package hellojpa;

import javax.persistence.Entity;

@Entity
public class Book extends Item{
    private String author;
    private String isbn;
}

Item.java

package hellojpa;

import javax.persistence.*;

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Item {
    @Id
    @GeneratedValue
    private Long id;

    private String name;
    private int price;

    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;
    }
}

JpaMain.java

package hellojpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class JpaMain {

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

        EntityManager em = emf.createEntityManager();

        EntityTransaction tx = em.getTransaction();
        tx.begin();

        try{

            Movie movie = new Movie();
            movie.setDirector("aaaa");
            movie.setActor("bbbb");
            movie.setName("바람과 함께 사라지다");
            movie.setPrice(10000);

            tx.commit();
        }catch(Exception e){
            tx.rollback();
        } finally{
            em.close();
        }
        emf.close();
    }
}

Locker.java

package hellojpa;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Locker {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    @OneToOne(mappedBy = "locker")
    private Member member;
}

Member.java

package hellojpa;

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

@Entity
public class Member {
    @Id @GeneratedValue
    @Column(name = "MEMBER_ID")
    private Long id;

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

    @ManyToOne
    @JoinColumn(name = "Team_ID",insertable = false, updatable = false)
    private Team team;

    @OneToOne
    @JoinColumn(name ="LOCKER_ID")
    private Locker locker;

    @OneToMany(mappedBy = "member")
    private List<MemberProduct> memberProducts = new ArrayList<>();

    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;}
}

MemberProduct.java

package hellojpa;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
public class MemberProduct {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne
    @JoinColumn(name="MEMBER_ID")
    private Member member;

    @ManyToOne
    @JoinColumn(name = "PRODUCT_ID")
    private Product product;

    private int count;
    private int price;
    private LocalDateTime orderDateTime;
}

Movie.java

package hellojpa;

import javax.persistence.Entity;

@Entity
public class Movie extends Item{

    private String director;
    private String actor;

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public String getActor() {
        return actor;
    }

    public void setActor(String actor) {
        this.actor = actor;
    }
}

Product.java

package hellojpa;

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

@Entity
public class Product {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    @OneToMany (mappedBy = "product")
    private List<MemberProduct> memberProducts = 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;
    }
}

RoleType.java

package hellojpa;

public enum RoleType {
    GUEST, USER, ADMIN
}

Team.java

package hellojpa;

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

@Entity
public class Team {

    @Id @GeneratedValue
    @Column(name = "TEAM_ID")
    private Long id;
    private String name;

    @OneToMany
    @JoinColumn(name = "TEAM_ID")
    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;
    }
}

persistence.xml

<?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">
    <persistence-unit name="hello">
        <properties>
            <!-- 필수 속성 -->
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.user" value="sa"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>

            <!-- 옵션 -->
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.use_sql_comments" value="10"/>
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>
    </persistence-unit>
</persistence>

内容的提问来源于stack exchange,提问作者이소정

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 17:20:41