2016-10-19 82 views
0

我想用java和hibernate開發我的第一個簡單的webapp,但它不起作用。錯誤org.hibernate.MappingException:未知的實體:com.csm.app1st.model.User

這是錯誤:

org.hibernate.MappingException: Unknown entity: com.csm.app1st.model.User 
    at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620) 
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1606) 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) 
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) 
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) 
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:675) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:667) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:662) 
    at com.csm.app1st.controller.AddUser.doPost(AddUser.java:40) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1083) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Unknown Source) 

這是我的代碼

的adduser:

package com.csm.app1st.controller; 

import java.io.IOException; 

import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 

import com.csm.app1st.model.User; 

/** 
* Servlet implementation class AddUser 
*/ 
@WebServlet(description = "Create new user Servlet", urlPatterns = { "/CreateUser.do" }) 
public class AddUser extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     Configuration config = new Configuration().configure(); 
     ServiceRegistry servReg = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build(); 
     SessionFactory factory = config.buildSessionFactory(servReg); 

     Session session = factory.openSession(); 
     session.beginTransaction(); 
     User u = new User(request.getParameter("firstname"), request.getParameter("lastname"), request.getParameter("country")); 
     session.save(u); 
     session.getTransaction().commit(); 
     session.close(); 

     RequestDispatcher view = request.getRequestDispatcher("useradd.jsp"); 
     view.forward(request, response); 
    } 

} 

用戶

package com.csm.app1st.model; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name = "user") 

public class User { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name="user_id", nullable=false, unique=true) 
private int id; 

@Column(name="firstname", length=40, nullable=false) 
private String firstname; 

@Column(name="lastname", length=40, nullable=false) 
private String lastname; 

@Column(name="country", length=40, nullable=true) 
private String country; 

public User(String firstname, String lastname, String country) { 
    this.firstname = firstname; 
    this.lastname = lastname; 
    this.country = country; 
} 

get & set... 

} 

配置文件冬眠

<hibernate-configuration> 
    <session-factory> 

     <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> 
     <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> 

     <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/firstapp</property> 
     <property name="hibernate.connection.username">postgres</property> 
     <property name="hibernate.connection.password">password</property> 
     <property name="show_sql">false</property> 

     <mapping class="com.csm.app1st.model.User" /> 

    </session-factory> 
</hibernate-configuration> 

我不知道爲什麼它不起作用。 你能幫我嗎?

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>first_project</groupId> 
    <artifactId>first_project</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <resources> 
     <resource> 
     <directory>src</directory> 
     <excludes> 
      <exclude>**/*.java</exclude> 
     </excludes> 
     </resource> 
    </resources> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.1</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.4</version> 
     <configuration> 
      <warSourceDirectory>WebContent</warSourceDirectory> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 
+0

您的hibernate配置文件名爲'hibernate.cfg.xml'嗎?它放在你的項目中的位置是什麼? – pleft

+0

這可能不會解決你當前的問題,但是我發現一個錯誤:Hibernate實體allways需要一個沒有參數的默認構造函數。從我看到你的用戶類缺少這個默認的構造函數。 –

+0

是的配置文件的名稱是hibernate.cfg.xml,是一個問題?該文件的位置是:Java Resources/src – Scarj

回答

0

由於您使用的註解,你應該改變這一行:

Configuration config = new Configuration().configure(); 

Configuration config = new AnnotationConfiguration().configure(); 

一個非常簡單的例子幾乎相同,你可以找到:http://www.concretepage.com/hibernate/example_annotationconfiguration_hibernate

+0

我做到了,但它不工作,這是錯誤: 在該行 \t多個標記 - 的方法來配置()是未定義的類型AnnotationConfiguration – Scarj

+0

奇怪,你使用的是哪個版本的hibernate?你有'pom.xml'文件發佈也在你的問題? – pleft

+0

我正在使用hibernate 5.2.3,我添加了pom.xml – Scarj

1

我試着用

SessionFactory factory = new Configuration().configure() 
       .buildSessionFactory(); 

其餘全部same.It的更換工作將下面的代碼adduser.java文件

Configuration config = new Configuration().configure(); 
ServiceRegistry servReg = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build(); 
SessionFactory factory = config.buildSessionFactory(servReg); 

我。

相關問題