我已将支持jar和jdbc驱动程序添加到我的项目中,但仍然是我的异常
无法找到结果集异常 执行load命令时出错:获取sqlgrammerexception
我的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="com.util.Product"/>
</session-factory>
</hibernate-configuration
entity class
@Entity
@Table(name="productdetails")
public class Product implements Serializable {
@Id
@Column(name="product-id")
private int id;
@Column(name="product-name")
private String name;
@Column(name="product-description")
private String description;
@Column(name="product-price")
private float price;
public Product() {
super();
}
public Product(int id, String name, String description, float price) {
super();
this.id = id;
this.name = name;
this.description = description;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
public class Executingclass {
public static void main(String args[]) {
SessionFactory sessionFactory=Hibernateutil.getSessionFactory();
Session session=sessionFactory.openSession();
Product product=(Product)session.get(Product.class, 1);
System.out.println(product.getId());
System.out.println(product.getName());
System.out.println(product.getDescription());
System.out.println(product.getPrice());
}
}
public class Hibernateutil {
static SessionFactory sessionFactory=null;
static{
Configuration configuration=new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory=configuration.buildSessionFactory(serviceRegistry);
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
错误:
INFO: HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:91)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:449)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:202)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1106)
at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2587)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:991)
at com.util.Executingclass.main(Executingclass.java:10)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'product0_.product' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2470)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2617)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2550)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1962)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
... 16 more
请帮我解决这个问题我是hibernate的新手
答案 0 :(得分:4)
在堆栈跟踪中,有一行指向问题:
import java.util.concurrent.TimeUnit;
public class StopThread {
private static boolean stopRequested;
public static void main(String[] args)
throws InterruptedException {
Thread backgroundThread = new Thread(new Runnable() {
public void run() {
int i = 0;
while (!stopRequested)
i++;
}
});
backgroundThread.start();
TimeUnit.SECONDS.sleep(1);
stopRequested = true;
}
}
如果您看到未知列,我首先想到的是,类中字段的映射存在一些错误。
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'product0_.product' in 'field list'
当你在上面提到的堆栈跟踪中看到这一点时,你认为,好吧,没有product0_.product列名,最接近的只是产品,后跟连字符,也许是连字符它带来了什么麻烦。
在谷歌搜索后,我从另一个问题中找到了这个answer,指出使用连字符是可能的,但它有一个特殊要求,也许hibernate没有正确处理这个问题。
总而言之,在模式中,在两个地方,数据库和@Id
@Column(name="product-id")
private int id;
@Column(name="product-name")
private String name;
@Column(name="product-description")
private String description;
@Column(name="product-price")
private float price;
中尝试不带连字符。
答案 1 :(得分:0)
我在application.properties文件中使用了以下属性。 我们需要告诉spring boot我们的隐式命名策略。因此使用了以下属性。
还检查了您使用的是CrudRepository还是JPARepository,因为我的解决方案适用于JPA
# %bb.2: # %for.body
# in Loop: Header=BB0_1 Depth=1
.loc 1 2333 0 # myls.c:2333:0
movl -4(%ebp), %eax
.Ltmp4:
.loc 1 71 14 # myls.c:71:14
movl %eax, -8(%ebp)
.Ltmp5:
.LBB0_3: # %for.cond1
# Parent Loop BB0_1 Depth=1
# => This Inner Loop Header: Depth=2
.loc 1 2333 0 # myls.c:2333:0
movl -8(%ebp), %eax
.Ltmp6:
.loc 1 71 18 # myls.c:71:18
cmpl n, %eax
.Ltmp7:
.loc 1 23333 1 # myls.c:23333:1
jge .LBB0_13
......
LBB2_10: # %sw.epilog
.loc 1 23333 1 # myls.c:23333:1
jmp .LBB2_11
.LBB2_11: # %while.cond
# =>This Inner Loop Header: Depth=1
.loc 1 162 13 # myls.c:162:13
cmpl $0, -164(%ebp)
.loc 1 23333 1 # myls.c:23333:1
jl .LBB2_20
# %bb.12: # %while.body
# in Loop: Header=BB2_11 Depth=1
.Ltmp75:
.loc 1 164 14 # myls.c:164:14
movl -136(%ebp), %eax
.loc 1 164 28 is_stmt 0 # myls.c:164:28
movl -164(%ebp), %ecx
# kill: def $cl killed $ecx
.loc 1 164 25 # myls.c:164:25
movl $1, %edx
shll %cl, %edx
.loc 1 164 22 # myls.c:164:22
andl %edx, %eax
cmpl $0, %eax
.Ltmp76:
.loc 1 23333 1 is_stmt 1 # myls.c:23333:1
je .LBB2_18
和
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
之前出现错误
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl