【问题标题】:Spring + Hibernate annotations error "is not mapped"Spring + Hibernate 注释错误“未映射”
【发布时间】:2012-12-07 08:08:27
【问题描述】:

我使用带有注释的 Spring + Hibernate,但出现以下错误:

org.hibernate.hql.ast.QuerySyntaxException: Produit is not mapped [from Produit]

当我调用这个函数时它会出现:

public List<Produit> getListeProduit() {

    return sessionFactory.getCurrentSession().createQuery("from Produit").list();
}

这是我的 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <mapping class="port.domain.Produit" />
</session-factory>     
</hibernate-configuration>

使用@Entity、@Table 很好地注释产品类 带有@Id、@Column、@GeneratedValue 的 ID @Column 的其他列

这是我的 XXX-servlet.xml 中的 bean SessionFactory:

<bean id="sessionFactory"     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
</bean>

编辑:实体代码

@Entity
@Table(name="produit")
public class Produit implements Serializable{

@Id
@Column(name="produit_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int produitId;

@Column(name="produit_nom")
private String produitNom;

public void setProduitId(int i) {
    produitId = i;
} 

public int getProduitId() {
    return produitId;
} 

public void setProduitNom(String s) {
    produitNom = s;
} 

public String getProduitNom() {
    return produitNom;
} 
}

我知道有很多关于这个问题的帖子,但我没有找到任何正确的问题。 我知道 Hibernate 无法映射我的课程,但我不知道为什么...

问题可能出在哪里?

非常感谢。

【问题讨论】:

  • 请贴出实体代码
  • 我在帖子中添加了编辑
  • 愚蠢的问题,但是 mysql 实例是在 Unix 机器还是 windows 上?
  • Mysql 实例在 Windows 上
  • 好的,如果我的 sql 在 unix 上,这似乎是个问题,stackoverflow.com/questions/10093718/…

标签: spring hibernate annotations


【解决方案1】:

通常问题很简单:您应该使用javax.persistence.Entity 而不是Hibernate 特定的org.hibernate.annotations.Entity。后者在 Hibernate 中被弃用,在可能的情况下支持 JPA 注释。

这正是你没有展示的,所以希望这是一个幸运的镜头:)

【讨论】:

  • 天哪……它终于起作用了,你是对的……为什么org.hibernate.annotations.Entity不起作用?
  • 它已被弃用,Hibernate 现在尽可能使用 JPA 特定的注解。
  • 所以对于 Hibernate,我必须在我的实体中更喜欢 JPA 注释而不是休眠注释?
  • 没错。当 JPA 中没有类似物时,Hibernate 会回退到它自己的注释。或者两种方式都不时起作用,对于这样的注释处理,您可以在 Hibernate 中找到“如果这是特定于 JPA 的,那么这种行为。否则 - 这种行为'。
猜你喜欢
  • 1970-01-01
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 2022-12-03
  • 2018-11-23
  • 1970-01-01
相关资源
最近更新 更多