gpt4 book ai didi

java - Hibernate 表未映射

转载 作者:行者123 更新时间:2023-12-02 09:00:26 25 4
gpt4 key购买 nike

我有实体

@Entity
@Table(
name = SomeEntity.TABLE_NAME,
uniqueConstraints = {@UniqueConstraint(columnNames = {"first", "second"})}
)
public class SomeEntity extends SomeSuperEntity {
public static final String TABLE_NAME = "SomeEntity";

public SomeEntity() {
}

public SomeEntity(String first, String second, Integer third) {
super(first, second, third);
}
}

该实体的父类(super class)

@MappedSuperclass
public abstract class SomeSuperEntity extends SomeSuperAbstractEntity {
@Column(nullable = false)
private String first;
@Column(nullable = false)
private String second;

protected SomeSuperEntity () {
}

protected SomeSuperEntity (String first, String second, Integer third) {
super(third);
this.first= first;
this.second= second;
}
}

第二个 super 类

@MappedSuperclass
public abstract class SomeSuperAbstractEntity {
@Column(nullable = false)
private Integer third;

protected SomeSuperAbstractEntity () {
}

protected SomeSuperAbstractEntity (Integer third) {
this.third= third;
}
}

我有这两种方法,第一个 - 用于查找实体

public <T extends SomeSuperEntity > Optional<T> findByFirstAndSecond(String first, String second, Class<T> tClass) {
String hql = String.format("FROM %s E WHERE E.first = :first AND E.second = :second", getTable(tClass));
return jpaApi.em().createQuery(hql, tClass)
.setParameter("first", first)
.setParameter("second", second)
.getResultList().stream().findAny();
}

第二个用于获取实体名称到 hql

private String getTable(Class<? extends SomeSuperEntity > tClass) {
if (tClass.equals(SomeEntity .class)) {
return SomeEntity.TABLE_NAME;
}
return null;
}

当我尝试调用 findByFirstAndSecond("1", "2", SomeEntity.class) 时,出现错误

[错误] c.i.SomeMethod- 错误:org.hibernate.hql.internal.ast.QuerySyntaxException:SomeEntity 未映射 [FROM SomeEntity E WHERE E.first = :first AND E.second = :second]

最佳答案

您是否定义了要在应用程序上下文中扫描的包?

<property name="packagesToScan">
<list>
<value>com.package_to_someentity</value>
</list>
</property>

或者,在您的 hibernate.cfg.xml 中定义它

<hibernate-configuration>
<session-factory>
<mapping class="com.package_to_someentity.SomeEntity" />
</session-factory>
</hibernate-configuration>

关于java - Hibernate 表未映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60210991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com