gpt4 book ai didi

java - 错误 :org. hibernate.hql.internal.ast.QuerySyntaxException: Website is not mapped

转载 作者:行者123 更新时间:2023-12-04 20:48:15 25 4
gpt4 key购买 nike

我正在尝试使用 restful web 服务创建项目。我收到错误

org.hibernate.hql.internal.ast.QuerySyntaxException: Website is not mapped

。我已经尝试了每种解决方案来解决此错误。请查看我的代码,如果有任何建议,请告诉我。谢谢

Gif Controller :

  package com.gif.code.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.gif.code.model.GifModel;
import com.gif.code.service.GifService;

@RestController
public class GifController {

@Autowired
GifService gifService;

@RequestMapping(value = "/getAllData", method = RequestMethod.GET, headers = "Accept=application/json")
public List<GifModel> getData() {
System.out.println("In Controller");
List<GifModel> listOfData = gifService.getAllData();
return listOfData;
}

@RequestMapping(value = "/getData/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public GifModel getDataById(@PathVariable int id) {
return gifService.getData(id);
}

@RequestMapping(value = "/addData", method = RequestMethod.POST, headers = "Accept=application/json")
public void addData(@RequestBody GifModel gifModel) {
gifService.addData(gifModel);

}

}

Gif模型:

    package com.gif.code.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="WEBSITE")
public class GifModel {

@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;

@Column(name="title")
String title;

@Column(name="description")
String description;

public GifModel() {
super();
}
public GifModel(int i, String title,String description) {
super();
this.id = i;
this.title = title;
this.description=description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

Gif 服务:

    package com.gif.code.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gif.code.dao.GifDao;
import com.gif.code.model.GifModel;

@Service("gifService")
public class GifService {

@Autowired
GifDao gifDao;

@Transactional
public List<GifModel> getAllData() {
System.out.println("In Service");
return gifDao.getAllData();
}

@Transactional
public GifModel getData(int id) {
return gifDao.getData(id);
}

@Transactional
public void addData(GifModel gifmodel) {
gifDao.addData(gifmodel);
}

}

GifDao:

package com.gif.code.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.gif.code.model.GifModel;

@Repository
public class GifDao {

@Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sf) {
this.sessionFactory = sf;
}

@SuppressWarnings("unchecked")
public List<GifModel> getAllData() {
System.out.println("In Dao");
Session session = this.sessionFactory.getCurrentSession();
List<GifModel> textList = session.createQuery("from Website").list();
return textList;
}

public GifModel getData(int id) {
Session session = this.sessionFactory.getCurrentSession();
GifModel gifmodel = (GifModel) session.load(GifModel.class, new Integer(id));
return gifmodel;
}

public GifModel addData(GifModel gifmodel) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(gifmodel);
return gifmodel;
}

}

Spring-servlet.xml:

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

<annotation-driven />

<resources mapping="/resources/**" location="/resources/" />

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/gifdata" />
<beans:property name="username" value="root" />
<beans:property name="password" value="Chirpn123" />
</beans:bean>

<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.gif.code.model.GifModel</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>

<context:component-scan base-package="com.gif.code" />

<tx:annotation-driven transaction-manager="transactionManager"/>

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


</beans:beans>

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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gif.code</groupId>
<artifactId>Gif</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Gif Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.0.RELEASE</version>
</dependency> -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>

<!-- Apache Commons DBCP -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<finalName>Gif</finalName>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>

</build>
<properties>
<spring.version>4.2.1.RELEASE</spring.version>
<security.version>4.0.3.RELEASE</security.version>
<jdk.version>1.7</jdk.version>
<hibernate.version>4.3.5.Final</hibernate.version>
<org.aspectj-version>1.7.4</org.aspectj-version>
</properties>
</project>

最佳答案

HQL 使用类名而不是表名!

所以你必须写:

List<GifModel> textList = session.createQuery("from GifModel").list();

关于java - 错误 :org. hibernate.hql.internal.ast.QuerySyntaxException: Website is not mapped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41996349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com