首页 新闻 赞助 找找看

Hibernate继承关系映射,出现严重的bug,加上属性schema指定数据库名称,提示找不到表?

0
悬赏园豆:5 [待解决问题]

在基类的映射文件中,加上属性schema指定数据库test,执行代码保存数据,结果提示找不到表,但是去掉schema则可以正常插入数据,为什么?

基类映射配置文件:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="priv.lwx.hibernate.basic.entity">
    <class name="Product" table="product" schema="test">
        <id name="id" type="int">
            <column name="id" unique="true"/>
            <!--主键值的生成方式-->
            <generator class="identity"/>
        </id>
        <property name="name" type="string">
            <column name="name" length="50" not-null="true"/>
        </property>
        <property name="price" type="big_decimal">
            <column name="price" not-null="true"/>
        </property>
        <joined-subclass name="Book" table="book">
            <key column="product_id"/>
            <property name="author" type="string" column="author"/>
            <property name="publisher" type="string" column="publisher"/>
        </joined-subclass>
    </class>
</hibernate-mapping>

基类:

package priv.lwx.hibernate.basic.entity;

import java.math.BigDecimal;

public class Product {
  private Integer id;
  private String name;
  private BigDecimal price;

  public Product() {
  }

  public Product(Integer id, String name, BigDecimal price) {
    this.id = id;
    this.name = name;
    this.price = price;
  }

  public Integer getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public BigDecimal getPrice() {
    return price;
  }

  public void setPrice(BigDecimal price) {
    this.price = price;
  }
}

子类:

package priv.lwx.hibernate.basic.entity;

/**
 * description
 *
 * @author liaowenxiong
 * @date 2022/11/5 21:10
 */

public class Book extends Product {
  private String author;
  private String publisher;


  public String getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }

  public String getPublisher() {
    return publisher;
  }

  public void setPublisher(String publisher) {
    this.publisher = publisher;
  }

}

测试类:

package priv.lwx.hibernate.basic.entity;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.jupiter.api.Test;
import priv.lwx.hibernate.basic.util.HibernateUtils;

import java.math.BigDecimal;

/**
 * 继承关系映射测试
 *
 * @author liaowenxiong
 * @date 2022/11/5 21:20
 */

public class ExtendsTest {
  @Test
  public void saveProduct() {
    Session session = HibernateUtils.getSession();
    Transaction tx = session.beginTransaction();
    Book book = new Book();
    book.setName("What is Java");
    book.setPrice(new BigDecimal("200.00"));
    book.setAuthor("Steven");
    book.setPublisher("Earth Publisher");
    session.save(book);
    tx.commit();
    HibernateUtils.closeSession();
  }
}

结果:

Caused by: org.hibernate.AssertionFailure: Table test.product not found
	at org.hibernate.persister.entity.AbstractEntityPersister.getTableId(AbstractEntityPersister.java:5608)
	at org.hibernate.persister.entity.JoinedSubclassEntityPersister.<init>(JoinedSubclassEntityPersister.java:450)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
	at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
	... 73 more
liaowenxiong的主页 liaowenxiong | 初学一级 | 园豆:33
提问于:2022-11-05 22:59
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册