1

我收到这个错误。

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);
    }
}

我读过很多帖子,这与不在字段和吸气剂上同时设置注释有关。但正如你所看到的,这不是原因。我难住了。

<?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。但这没有任何区别。

4

2 回答 2

2

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

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


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

评论:你不应该注释/使用你的实体作为@Component

于 2013-08-29T11:56:35.270 回答
0

您应该从标签中删除前导斜杠 (" /") 字符,并且您的属性应该是,而不是。<context:component-scan>packagesToScansessionFactory*/

于 2013-08-29T11:52:17.937 回答