使用Spring和Hibernate我得到了这个例外:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'user0_.company_ID' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
这是我的映射文件:
<context:annotation-config />
<context:component-scan base-package="permissionsPackage.Entity" />
<context:component-scan
base-package="permissionsPackage.dao" />
<bean id="UserPermissionsDao" class="permissionsPackage.dao.UserPermissionsDao"/>
<bean id="UserContextDao" class="permissionsPackage.dao.UserContextDao"/>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/permissions?autoReconnect=true"
p:username="root"
p:password="root"></bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="permissionsPackage.Entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="session" class="org.springframework.orm.hibernate4.SessionFactoryUtils" factory-method="getSession" scope="prototype">
<constructor-arg ref="sessionFactory" />
<constructor-arg value="false" />
<!-- This is seems to be needed to get rid of the 'No Hibernate Session' error' -->
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
我的用户实体如下所示:
@Entity(name = "permissionsPackage.Entity.User")
@Table(name = "User")
public class User {
private long Id;
@Column(name = "NAME")
private String name;
@Column(name = "COMPANYID")
private long companyId;
boolean hasPermission;
public User(Company company, long companyId, String name) {
this.company = company;
this.companyId = companyId;
this.name = name;
}
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return Id;
}
public void setId(long id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getCompanyId() {
return companyId;
}
public void setCompanyId(long companyId) {
this.companyId = companyId;
}
我只想创建一个简单的查询,从User中提取所有细节。像这样:
String hql = "FROM permissionsPackage.Entity.User";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
List results = query.list();
return (User) results;
我不明白为什么它说它找到了列,因为我可以插入表中?