gpt4 book ai didi

java - 从另一个包添加实体时为 "QuerySyntaxException: User is not mapped"

转载 作者:行者123 更新时间:2023-11-30 12:03:19 25 4
gpt4 key购买 nike

我已经创建了一个 Hibernate 配置 maven 项目(使用 maven-shade-plugin 构建)以允许我的代码库使用一个统一的“数据库类”。但是,当从正在运行的项目外部调用时,该类似乎没有映射(在运行时抛出 org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped 异常)。

在项目中运行时,一切正常。但是,当从项目外部运行时,Hibernate 无法映射实体。


CommonDB.java(maven包meta1203-data的一部分)

package com.meta1203.microservices;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class CommonDB <C extends BaseEntity> {
private Configuration cfg;
private SessionFactory sf;
private Session session;
private Class<C> c;

public CommonDB(Class<C> anoClass) {
String jdbcUrl = String.format(
"jdbc:mysql://%s/%s",
System.getenv("DB_URL"),
System.getenv("DB_NAME"));

cfg = new Configuration()
.setProperty("hibernate.connection.url", jdbcUrl)
.setProperty("hibernate.connection.username", System.getenv("DB_USERNAME"))
.setProperty("hibernate.connection.password", System.getenv("DB_PASSWORD"))

.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
.setProperty("hibernate.connection.pool_size", "1")
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect")
.setProperty("hibernate.hbm2ddl.auto", "update")
.setProperty("hibernate.show_sql", "true")
.addAnnotatedClass(anoClass.getClass());

c = anoClass;
}

public void open() {
sf = cfg.buildSessionFactory();
session = sf.openSession();
}

public void close() {
session.close();
sf.close();
}

public C findOneBy(String field, String o) {
String query = "select u from " + c.getSimpleName() + " u where u." + field + " = :id";

return session.createQuery(query, c).setParameter("id", o).getSingleResult();
}
// other CRUD functions
}

TestDB.java(maven包meta1203-userservice的一部分)

package com.meta1203.microservices.user;

import com.meta1203.microservices.CommonDB;
import com.meta1203.microservices.user.model.User;

public class TestDB {
public static void main(String[] args) {
TestDB tdb = new TestDB();
CommonDB<User> db = new CommonDB<User>(User.class);
db.open();

User hunter = db.findOneBy("username", "hunter");
System.out.println(hunter.getUsername());
System.out.println(db.countBy("username", "hunter"));

db.close();
}
}

User.java(maven 包 meta1203-userservice 的一部分)

package com.meta1203.microservices.user.model;

import java.util.Set;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Table;

import com.meta1203.microservices.BaseEntity;

@Entity
@Table(name = "users")
public class User extends BaseEntity {
private String username;

private String textNotificationList;
private String emailNotificationList;

@ElementCollection
private Set<Long> ignoredAlerts;
@ElementCollection
private Set<Long> clients;

// to be parsed with DateTimeFormatter.ISO_OFFSET_DATE_TIME
private String lastItineraryUpdate;

// getters and setters
}

父级的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.meta1203</groupId>
<artifactId>meta1203-services</artifactId>
<packaging>pom</packaging>
<version>0.0.1</version>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.meta1203</groupId>
<artifactId>meta1203-data</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.11.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
</dependencies>
<modules>
<module>meta1203-userservice</module>
</modules>
</project>

Sep 07, 2019 2:36:13 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {[WORKING]}
Sep 07, 2019 2:36:13 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 07, 2019 2:36:13 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
Sep 07, 2019 2:36:14 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Sep 07, 2019 2:36:14 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@329dbdbf] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Sep 07, 2019 2:36:14 AM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select u from User u where u.username = :id]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:729)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:745)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:104)
at com.meta1203.microservices.CommonDB.findOneBy(CommonDB.java:72)
at com.meta1203.microservices.user.TestDB.main(TestDB.java:12)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select u from User u where u.username = :id]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:219)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:119)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:611)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:720)
... 4 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:169)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3695)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3584)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:271)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:191)
... 10 more

最佳答案

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

您的实体名为 User,但它映射到一个名为 users 的表。

将实体更改为用户或将表更改为用户。

应该这样做。

关于java - 从另一个包添加实体时为 "QuerySyntaxException: User is not mapped",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57830387/

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