问题出在 Hibernate 映射上(将注解改为 XML)。

huangapple 未分类评论44阅读模式
英文:

Problem with hibernate mapping (change anotations with xml)

问题

我有一个使用Spring和Hibernate的应用程序,它在使用JPA注解时运行良好,但我想要用XML配置来替代这些注解。但是我遇到了以下错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [FROM User]
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [FROM User]

User.java
```java
package com.fp.models;

public class User implements UserDetails {

    private long id;

    private String username;

    private String password;

    private Set<Role> authorities;

    public User() {
    }

User.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.fp.models.User" table="users">
        <id name="id" type="long" column="user_id">
            <generator class="native"/>
        </id>
        <property name="username" column="username" type="string"/>
        <property name="password" column="password" type="string"/>
        <set name="authorities" table="users_roles"
             inverse="false" lazy="true" fetch="select" cascade="all" >
            <key>
                <column name="user_id" not-null="true" />
            </key>
            <many-to-many entity-name="com.fp.models.Role">
                <column name="role_id" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>

HibernateConfiguration.java -> 链接
这是我使用注解的配置。

测试方法:

@Override
public int listAllUsers() {
    try (Session session = sessionFactory.openSession()) {
        String hql = "FROM User";
        Query query = session.createQuery(hql);
        if (query.list().size() == 0) {
            return 0;
        } else {
            return 1;
        }
    } catch (HibernateException he) {
        System.out.println(he.getMessage());
        throw he;
    }
}

<details>
<summary>英文:</summary>

I have a spring-hibernate application which work fine with JPA anotations, but I want to change anotations with xml configuration, but but I get this error: 

org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [FROM User]
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [FROM User]

User.java

package com.fp.models;

public class User implements UserDetails {

private long id;

private String username;

private String password;

private Set&lt;Role&gt; authorities;

public User() {
}
User.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.fp.models.User" table="users">
<id name="id" type="long" column="user_id">
<generator class="native"/>
</id>
<property name="username" column="username" type="string"/>
<property name="password" column="password" type="string"/>
<set name="authorities" table="users_roles"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key>
<column name="user_id" not-null="true" />
</key>
<many-to-many entity-name="com.fp.models.Role">
<column name="role_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>

HibernateConfiguration.java -&gt; &lt;a&gt; https://pastebin.com/DrcGSBAp
this is the configuration which i use with the anotations

Method for test:

@Override
public int listAllUsers() {
try (Session session = sessionFactory.openSession()) {
String hql = "FROM User";
Query query = session.createQuery(hql);
if (query.list().size() == 0) {
return 0;
} else {
return 1;
}
} catch (HibernateException he) {
System.out.println(he.getMessage());
throw he;
}
}



</details>


huangapple
  • 本文由 发表于 2020年4月9日 05:17:31
  • 转载请务必保留本文链接:https://java.coder-hub.com/61110185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定