错误 SchemaUpdate:237 - 靠近“来自”:语法错误

Posted

技术标签:

【中文标题】错误 SchemaUpdate:237 - 靠近“来自”:语法错误【英文标题】:ERROR SchemaUpdate:237 - near "from": syntax error 【发布时间】:2015-07-14 12:14:26 【问题描述】:

我是 Spring 新手,我只是想知道这些错误是什么意思?

ERROR SchemaUpdate:236 - HHH000388: Unsuccessful: create table FacebookPosts (id  integer, created_time timestamp not null, from varchar not null, message varchar not null, picture_url varchar not null, post_id varchar not null, to varchar not null, updated_time timestamp not null, primary key (id))
ERROR SchemaUpdate:237 - near "from": syntax error

我正在使用 Spring Social 的 Facebook 库来获取我最近的帖子并将它们存储在 SQLite 数据库中。

我的代码如下:

Facebook 控制器

package adam.social.media.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.Post;
import org.springframework.social.facebook.api.impl.FacebookTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import adam.social.media.model.FacebookModel;
import adam.social.media.repository.FacebookRepository;

@Controller
public class FacebookController    

    private Facebook facebook;

    private FacebookRepository facebookRepository;

    private static Logger logger = Logger.getLogger(FacebookController.class
            .getName());

    @Autowired
    public FacebookController(FacebookRepository facebookRepository) 
        logger.debug("ENTERING FacebookController");

        this.facebookRepository = facebookRepository;               

        facebook = new FacebookTemplate("CAACEdEose0cBAGjDHb17dTdS4VJdhuL5ypxQppYWDKTDa963rn4rvZBoUfAtYlxcFzkf8aejL0c4cBvtR2OG0VjWB7bsBstq71SLzHB6d4CvgLmzJyi5kRAysZCFlj6TIFVhwfMZAHzYaDNyWiI3z7P5x8jPOvdUCXSR8pxmeqyeifnH46S5h0p1szuOL6uSdeQlAF3ZAP5esWtqS8GqG3LOSlfKSx4ZD");              

        logger.debug("EXITING FacebookController");
    

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView getFacebookStatus(FacebookModel facebookModel) 
        logger.debug("ENTERING getFacebookStatus");

        ModelAndView modelAndView = new ModelAndView("index");      

        Post post = facebook.feedOperations().getFeed().get(0);

        facebookModel.setPostId(post.getId());
        facebookModel.setTo(post.getTo().get(0).getName());
        facebookModel.setFrom(post.getFrom().getName());
        facebookModel.setCreatedTime(post.getCreatedTime());
        facebookModel.setUpdatedTime(post.getUpdatedTime());
        facebookModel.setMessage(post.getMessage());
        facebookModel.setPictureUrl(post.getPicture());

        modelAndView.addObject("facebookModel", facebookModel);

        logger.debug("EXITING getFacebookStatus");

        return modelAndView;
    

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView postFacebookStatus(FacebookModel facebookModel, BindingResult result) 
        logger.debug("ENTERING postFacebookStatus");

        if (result.hasErrors()) 
            Map<String, Object> params = new HashMap<String, Object>();

            params.put("facebookModel", facebookModel);

            return new ModelAndView("index", params);
        

        facebookRepository.saveAndFlush(facebookModel);

        logger.debug("EXITING postFacebookStatus");

        return new ModelAndView("redirect:/index.html");
    

Facebook 模型

package adam.social.media.model;

import java.io.Serializable;
import java.util.Date;

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

import org.apache.log4j.Logger;

@Entity(name = "FacebookPosts")
public class FacebookModel implements Serializable 

    private static final long serialVersionUID = -1L;

    private static Logger logger = Logger.getLogger(FacebookModel.class.getName());

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column(name = "post_id", nullable = false)
    private String postId;
    @Column(name = "to", nullable = false)
    private String to;
    @Column(name = "from", nullable = false)
    private String from;
    @Column(name = "created_time", nullable = false)
    private Date createdTime;
    @Column(name = "updated_time", nullable = false)
    private Date updatedTime;
    @Column(name = "message", nullable = false)
    private String message;
    @Column(name = "picture_url", nullable = false)
    private String pictureUrl;

    public int getId() 
        return id;
    

    public void setId(int id) 
        logger.debug("ENTERING setId");

        this.id = id;

        logger.debug("EXITING setId");
    

    public String getPostId() 
        return postId;
    

    public void setPostId(String postId) 
        logger.debug("ENTERING setPostId");

        this.postId = postId;

        logger.debug("EXITING setPostId");
    

    public String getTo() 
        return to;
    

    public void setTo(String to) 
        logger.debug("ENTERING setTo");

        this.to = to;

        logger.debug("EXITING setTo");
    

    public String getFrom() 
        return from;
    

    public void setFrom(String from) 
        logger.debug("ENTERING setFrom");

        this.from = from;

        logger.debug("EXITING setFrom");
    

    public Date getCreatedTime() 
        return createdTime;
    

    public void setCreatedTime(Date createdTime) 
        logger.debug("ENTERING setCreatedTime");

        this.createdTime = createdTime;

        logger.debug("EXITING setCreatedTime");
    

    public Date getUpdatedTime() 
        return updatedTime;
    

    public void setUpdatedTime(Date updatedTime) 
        logger.debug("ENTERING setUpdatedTime");

        this.updatedTime = updatedTime;

        logger.debug("EXITING setUpdatedTime");
    

    public String getMessage() 
        return message;
    

    public void setMessage(String message) 
        logger.debug("ENTERING setMessage");

        this.message = message;

        logger.debug("EXITING setMessage");
    

    public String getPictureUrl() 
        return pictureUrl;
    

    public void setPictureUrl(String pictureUrl) 
        logger.debug("ENTERING setPictureUrl");

        this.pictureUrl = pictureUrl;

        logger.debug("EXITING setPictureUrl");
    

    public int hashCode() 
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((createdTime == null) ? 0 : createdTime.hashCode());
        result = prime * result + ((from == null) ? 0 : from.hashCode());
        result = prime * result + id;
        result = prime * result + ((message == null) ? 0 : message.hashCode());
        result = prime * result
                + ((pictureUrl == null) ? 0 : pictureUrl.hashCode());
        result = prime * result + ((postId == null) ? 0 : postId.hashCode());
        result = prime * result + ((to == null) ? 0 : to.hashCode());
        result = prime * result
                + ((updatedTime == null) ? 0 : updatedTime.hashCode());
        return result;
    

    public boolean equals(Object obj) 
        if (this == obj) 
            return true;
        
        if (obj == null) 
            return false;
        
        if (getClass() != obj.getClass()) 
            return false;
        
        FacebookModel other = (FacebookModel) obj;
        if (createdTime == null) 
            if (other.createdTime != null) 
                return false;
            
         else if (!createdTime.equals(other.createdTime)) 
            return false;
        
        if (from == null) 
            if (other.from != null) 
                return false;
            
         else if (!from.equals(other.from)) 
            return false;
        
        if (id != other.id) 
            return false;
        
        if (message == null) 
            if (other.message != null) 
                return false;
            
         else if (!message.equals(other.message)) 
            return false;
        
        if (pictureUrl == null) 
            if (other.pictureUrl != null) 
                return false;
            
         else if (!pictureUrl.equals(other.pictureUrl)) 
            return false;
        
        if (postId == null) 
            if (other.postId != null) 
                return false;
            
         else if (!postId.equals(other.postId)) 
            return false;
        
        if (to == null) 
            if (other.to != null) 
                return false;
            
         else if (!to.equals(other.to)) 
            return false;
        
        if (updatedTime == null) 
            if (other.updatedTime != null) 
                return false;
            
         else if (!updatedTime.equals(other.updatedTime)) 
            return false;
        
        return true;
    

    public String toString() 
        return "FacebookModel [id=" + id + ", postId=" + postId + ", to=" + to
                + ", from=" + from + ", createdTime=" + createdTime
                + ", updatedTime=" + updatedTime + ", message=" + message
                + ", pictureUrl=" + pictureUrl + "]";
       

Facebook 存储库

package adam.social.media.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import adam.social.media.model.FacebookModel;

public interface FacebookRepository extends JpaRepository<FacebookModel, Integer> 


index.jsp

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<head>
    <title>Social Media</title>
</head>
<body>
    <form:form method="POST" commandName="facebookModel">
        <form:hidden path="id" />   
        <form:hidden path="postId" />   
        <form:hidden path="to" />   
        <form:hidden path="from" /> 
        <form:hidden path="createdTime" />  
        <form:hidden path="updatedTime" />  
        <form:hidden path="message" />  
        <form:hidden path="pictureUrl" />   

        <input name="submit" type="submit" value="Save" />
    </form:form>
</body>

mvc-context.xml

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:property-placeholder location="classpath:application.properties" />    
    <context:component-scan base-package="adam.social.media" />

    <mvc:annotation-driven />
    <mvc:view-controller path="/index.html" />

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
        p:basename="messages"/>

    <bean id="connectionFactoryLocator" class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
        <property name="connectionFactories">
            <list>
                <bean class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
                    <constructor-arg value="1054655621213643" />
                    <constructor-arg value="45c95ab39369187fa11b4272632ea4c7" />                
                </bean>
            </list>
        </property>
    </bean>

    <mvc:interceptors>    
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
            p:paramName="locale" />
    </mvc:interceptors>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix"><value>/WEB-INF/jsp/</value></property>
        <property name="suffix"><value>.jsp</value></property>
    </bean> 
</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>adam.social.media</groupId>
    <artifactId>SocialMedia</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Social Media</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-core</artifactId>
            <version>1.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-facebook</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.social</groupId>
            <artifactId>spring-social-web</artifactId>
            <version>1.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.6.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.7.2</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>SocialMedia</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
            </plugin>
        </plugins>
  </build>
</project>

提前致谢!

亚当

【问题讨论】:

【参考方案1】:

from 是一个reserved word in SQLite。您可以使用不同的名称(如果可能)或引用链接网址中提到的名称:

如果您想使用关键字作为名称,您需要引用它。那里 SQLite中引用关键字的四种方式:

'keyword' - 单引号中的关键字是字符串文字。 "keyword" - 双引号中的关键字是标识符。 [keyword] - 括在方括号中的关键字是标识符。这不是标准 SQL。使用了这种引用机制 由 MS 访问 和 SQL Server,并包含在 SQLite 中以实现兼容性。 `keyword` - 包含在重音符号(ASCII 代码 96)中的关键字是标识符。这不是标准 SQL。这种报价机制是 mysql 使用并包含在 SQLite 中以实现兼容性。

根据JPA spec(您需要接受他们的许可协议),似乎首选使用双引号:

@Column(name = "\"from\"", nullable = false)
private String from;

参照。 my related answer 了解更多(Hibernate 和 HSQL 特定)详细信息。

【讨论】:

以上是关于错误 SchemaUpdate:237 - 靠近“来自”:语法错误的主要内容,如果未能解决你的问题,请参考以下文章

Spring Server with H2 Database SchemaUpdate HHH000299

数据帧考拉到增量表:错误:调用 o237.save 时发生错误

SQLite错误(1):靠近“=”:语法错误

SQLiteDataReader 错误,靠近“表”:语法错误

HIbernate 模式更新错误

sqlite3.OperationalError:靠近“%”:语法错误