com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列' ....'

时间:2016-09-13 10:58:28

标签: hibernate spring-mvc

我有两个域 - 用户和目录......一个用户可以有很多目录。 这是我的User.java文件

package com.egs.account.model;


import org.apache.commons.lang.builder.EqualsBuilder;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String username;
    private String password;

    @Transient
    private String passwordConfirm;
    private String firstName;
    private String lastName;
    private Date dateRegistered;
    private Long skypeID;

    @ManyToMany
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles;

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private Set<Catalog> catalogs;

    public Long getId() {
        return id;
    }

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

    public String getUsername() { return username; }

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

    public String getPassword() {
        return password;
    }

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

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }

    public String getFirstName() { return firstName; }

    public void setFirstName(String firstName) { this.firstName = firstName; }

    public Date getDateRegistered() { return dateRegistered; }

    public void setDateRegistered(Date dateRegistered) { this.dateRegistered = dateRegistered; }

    public Long getSkypeID() { return skypeID; }

    public void setSkypeID(Long skypeID) { this.skypeID = skypeID; }

    public String getLastName() { return lastName; }

    public void setLastName(String lastName) { this.lastName = lastName; }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public Set<Catalog> getCatalogs() { return catalogs; }

    public void setCatalogs(Set<Catalog> catalogs) { this.catalogs = catalogs; }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (!(obj instanceof User)) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        final User other = (User) obj;
        final EqualsBuilder builder = new EqualsBuilder();
        builder.append(this.firstName, other.firstName);
        builder.append(this.lastName, other.lastName);
        builder.append(this.username, other.username);
        builder.append(this.skypeID, other.skypeID);
        return builder.isEquals();
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + firstName + ", lastName="
                + lastName + ", username=" + username + "]";
    }
}

这是我的Catalog.java文件

package com.egs.account.model;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "catalog")
public class Catalog {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String link;
    private String comment;
    private Date insertDate;

    @ManyToOne(optional = false)
    @JoinColumn(name = "USER_ID")
    private User user;

    @Lob @Basic(fetch = FetchType.LAZY)
    @Column(name="content", nullable=false)
    private byte[] content;

    public Long getId() {
        return id;
    }

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

    public String getLink() { return link; }

    public void setLink(String link) { this.link = link; }

    public String getComment() { return comment; }

    public void setComment(String comment) { this.comment = comment; }

    public Date getInsertDate() { return insertDate; }

    public void setInsertDate(Date insertDate) { this.insertDate = insertDate; }

    public User getUser() { return user;}

    public void setUser(User user) { this.user = user; }

    public byte[] getContent() { return content; }

    public void setContent(byte[] content) { this.content = content; }
}

这是我的abstractDao类,我从中扩展了我的存储库,那些使用它们 在服务类。在这里我调用了getSession()方法,因为我认为我得到了那个错误

 import org.hibernate.Criteria;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.transaction.annotation.Transactional;

    import java.io.Serializable;
    import java.lang.reflect.ParameterizedType;

    @Transactional
    public abstract class AbstractDao<PK extends Serializable, T> {

        private final Class<T> persistentClass;

        @SuppressWarnings("unchecked")
        public AbstractDao(){
            this.persistentClass =(Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        }

        @Autowired
        private SessionFactory sessionFactory;

        protected Session getSession(){
            return sessionFactory.getCurrentSession();
        }

        @SuppressWarnings("unchecked")
        public T getByKey(PK key) {
            return (T) getSession().get(persistentClass, key);
        }

        public void persist(T entity) {
            getSession().persist(entity);
        }

        public void delete(T entity) {
            getSession().delete(entity);
        }

        protected Criteria createEntityCriteria(){
            return getSession().createCriteria(persistentClass);
        }


    }

这是我的Hibernate配置

package com.egs.account.config;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.egs.account.config" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.egs.account" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        return properties;        
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }
}

当我试图保留我的用户域时,我得到了这样的例外。

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread

有人可以告诉我这个例外的原因。

1 个答案:

答案 0 :(得分:0)

所以我已经找到了我的问题的答案。 原因是 - 它在一个xml文件中被声明为名为“dataSource”的bean,并且它们之间存在冲突,因为“HibernateConfiguration”文件中存在“dataSource”配置方法。