编程语言
首页 > 编程语言> > java-无法从另一个Maven子项目中调用Hibernate / QueryDSL

java-无法从另一个Maven子项目中调用Hibernate / QueryDSL

作者:互联网

我有两个Maven子项目-jetty_runner1和jetty_runner2

我的目录结构如下所示:

./jetty_runner1/pom.xml
./jetty_runner1/src/main/java/com/jetty_runner1/CheckPersistence.java
./jetty_runner1/src/main/java/com/jetty_runner1/HelloWorldServlet.java
./jetty_runner1/src/main/java/com/jetty_runner1/MiscKeyValue.java
./jetty_runner1/src/main/java/com/jetty_runner1/MiscKeyValueManager.java
./jetty_runner1/src/main/resources/META-INF/persistence.xml
./jetty_runner1/src/main/webapp/WEB-INF/web.xml


./jetty_runner2/pom.xml
./jetty_runner2/src/main/java/com/jetty_runner2/CheckPersistence.java
./jetty_runner2/src/main/java/com/jetty_runner2/HelloWorldServlet.java
./jetty_runner2/src/main/resources/META-INF/persistence.xml
./jetty_runner2/src/main/webapp/WEB-INF/web.xml


./pom.xml

CheckPersistence.java,HelloWorldServlet.java,persistence.xml和web.xml在这两个项目中完全相同,除了包名称(我已经创建了此简化示例以在此处发布.在实际应用程序中,这两个子项目会千差万别的东西)

这是实际的文件内容:

CheckPersistence.java

public class CheckPersistence
{
    public void beginUpdation()
    {
        String val = null;
        List<MiscKeyValue> keyValues = MiscKeyValueManager.get().selectStar();
        if (keyValues != null && keyValues.size() == 1)
            val = keyValues.get(0).getStringValue();
        if (val == null)
            val = "Hello World";

        MiscKeyValue keyValue = new MiscKeyValue();
        keyValue.setStringKey("modifiedDate");
        keyValue.setStringValue(val);
        MiscKeyValueManager.get().put(keyValue);
    }
}

HelloWorldServlet.java

public class HelloWorldServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    {
        new CheckPersistence().beginUpdation();
    }
}

MiscKeyValue.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity (name="Misc_Key_Value")
public class MiscKeyValue
{
    @Id
    @Column(name="Misc_Key_Value_id")
    private String id;

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

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

    public String getId()
    {
        return id;
    }
    public void setId(String id)
    {
        this.id = id;
    }
    public String getStringKey()
    {
        return stringKey;
    }
    public void setStringKey(String stringKey)
    {
        this.stringKey = stringKey;
    }
    public String getStringValue()
    {
        return stringValue;
    }
    public void setStringValue(String stringValue)
    {
        this.stringValue = stringValue;
    }
}

MiscKeyValueManager.java

import java.util.List;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import com.mysema.query.jpa.impl.JPAQuery;

public class MiscKeyValueManager
{
    private static MiscKeyValueManager INSTANCE = new MiscKeyValueManager();
    protected EntityManagerFactory emf;
    public static QMiscKeyValue qmiscKeyValue = QMiscKeyValue.miscKeyValue;

    private MiscKeyValueManager()
    {
        this.emf = Persistence.createEntityManagerFactory("world");
    }

    public static MiscKeyValueManager get()
    {
        return INSTANCE;
    }

    public List<MiscKeyValue> selectStar()
    {
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        JPAQuery query = new JPAQuery(em);
        List<MiscKeyValue> results = query.from(qmiscKeyValue)
                .fetchAll().list(qmiscKeyValue);
        tx.commit();
        em.close();
        return results;
    }

    public MiscKeyValue put(MiscKeyValue e)
    {
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        if (e.getId() == null)
        {
            e.setId(UUID.randomUUID().toString());
        }
        MiscKeyValue mergedE = em.merge(e);
        em.flush();
        tx.commit();
        em.clear();
        em.close();
        return mergedE;
    }
}

persistence.xml中

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">
    <persistence-unit name="world">
      <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/world"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
        <property name="hibernate.format_sql" value="true"/>
      </properties>
    </persistence-unit>
</persistence>

web.xml中

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
   version="2.5"
   metadata-complete="true">
  <servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>com.jetty_runner1.HelloWorldServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/hello/*</url-pattern>
  </servlet-mapping>
</web-app>

每个pom.xml也包含QueryDSL和Jetty插件:

<build>
  <plugins>
   <plugin>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>${jettyVersion}</version>
    </plugin>
    <plugin>
      <groupId>com.mysema.maven</groupId>
      <artifactId>apt-maven-plugin</artifactId>
      <version>1.1.3</version>
      <executions>
        <execution>
          <goals>
            <goal>process</goal>
          </goals>
          <configuration>
            <outputDirectory>target/generated-sources/java</outputDirectory>
            <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>com.mysema.querydsl</groupId>
          <artifactId>querydsl-apt</artifactId>
          <version>${querydsl.version}</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

当我从jetty_runner1运行mvn jetty:run并访问URL localhost:8080 / hello /时,一切正常,就执行和更新查询而言.

当我从jetty_runner2运行相同的文件并命中相同的URL时,它给出了一个异常说:

org.hibernate.hql.internal.ast.QuerySyntaxException:未映射Misc_Key_Value [选择miscKeyValue
从Misc_Key_Value miscKeyValue获取所有属性]

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:218)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1821)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)
at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:129)
at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:96)
at com.mysema.query.jpa.impl.AbstractJPAQuery.list(AbstractJPAQuery.java:248)
at com.jetty_runner1.MiscKeyValueManager.selectStar(MiscKeyValueManager.java:35)
at com.jetty_runner2.CheckPersistence.beginUpdation(CheckPersistence.java:13)
at com.jetty_runner2.HelloWorldServlet.doGet(HelloWorldServlet.java:16)

解决方法:

因此,在进行更多搜索之后,我发现了两种解决方案:

1)将您的实体类添加到persistence.xml

2)使用org.hibernate.integrator.spi.Integrator服务,但是对于Hibernate 5,它的用法没有得到很好的记录

因此,#1目前具有一种刺激性:您需要手动将所有实体类放入persistence.xml中

标签:querydsl,java,maven,hibernate,jetty
来源: https://codeday.me/bug/20191013/1906629.html