org.hibernate.MappingException:无法确定typ

时间:2013-08-29 11:22:12

标签: java hibernate spring-mvc annotations

我收到了这个错误。

org.hibernate.MappingException: Could not determine type for: dom.Whore, at table:        Message, for columns: [org.hibernate.mapping.Column(receiver)]

这是映射到表中的类。

package dom;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.stereotype.Component;

@Component
@Entity
public class Message {

private Whore sender;
private Whore receiver;
private Date date = new Date();
private String messageText;
private Boolean read;
private long id;

public Message(){}

public Message(Whore sender, Whore receiver) {
    this.sender = sender;
    this.receiver = receiver;
}

public Whore getSender() {
    return sender;
}

public void setSender(Whore sender) {
    this.sender = sender;
}
public Whore getReceiver() {
    return receiver;
}

public void setReceiver(Whore receiver) {
    this.receiver = receiver;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}
public String getMessageText() {
    return messageText;
}

public void setMessageText(String messageText) {
    this.messageText = messageText;
}

public Boolean getRead() {
    return read;
}

public void setRead(Boolean read) {
    this.read = read;
}

@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy="increment")
public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}
}

这是无法确定类型的类。

package dom;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;


import org.hibernate.annotations.GenericGenerator;
import org.springframework.stereotype.Component;


@Component
@Entity
public class Whore {



    private String username;
    private String password;
    private String  email;
    private List<Whore> friends = new ArrayList<Whore>();
    private int reputation;
    private long id;
    private List<Message> messages = new ArrayList<Message>();



    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public int getReputation() {
        return reputation;
    }

    public void setReputation(int reputation) {
        System.out.println("in set reputation : " + reputation);
        this.reputation = this.reputation + reputation;
        System.out.println("new repuration : " + this.reputation);
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy="increment")
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    public List<Whore> getFriends() {
        return friends;
    }
    public void setFriends(List<Whore> friends) {
        this.friends = friends;
    }

    public void addFriend(Whore friend) {
        getFriends().add(friend);
    }

    public void removeFriend(Whore friend) {
        getFriends().remove(friend);
    }

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    public List<Message> getMessages() {
        return messages;
    }

    public void setMessages(List<Message> messages) {
        this.messages = messages;
    }

    public void addMessage(Message message) {
        getMessages().add(message);
    }
}

我在很多帖子中都读过,这与未在字段和getter上同时设置注释有关。但是你可以看到这不是原因。我很难过。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

        <context:component-scan base-package="/dom" />
        <context:component-scan base-package="/dao" />
        <context:component-scan base-package="/controllers" />
        <context:component-scan base-package="/services" />
        <context:component-scan base-package="/security" />
        <tx:annotation-driven /> 
        <mvc:annotation-driven />

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/culturewhore" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" /> 
        <property name="packagesToScan" value="/" />
        <property name="hibernateProperties">
            <props> 
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
                <util:list id="beanList">
                    <ref bean="mappingJacksonHttpMessageConverter" />
                </util:list>    
        </property>
    </bean>

    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="1000000" />

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />


</beans>

此外,我刚尝试在发送方和接收方的getter上使用@ManyToOne。但这没有任何区别。

2 个答案:

答案 0 :(得分:2)

您尚未将消息映射到消息中的妓女关系。应该是:

@ManyToOne
public Whore getSender() {
    return sender;
}


@ManyToOne
public Whore getReceiver() {
    return receiver;
}

评论:您不应将您的实体注释/使用为@Component

答案 1 :(得分:0)

您应该从/代码中删除前导斜杠(“<context:component-scan>”)字符,packagesToScan的{​​{1}}属性应为sessionFactory ,而不是*