gpt4 book ai didi

java - 为什么这个 String desc 变量没有在 hibernate 中插入到数据库中

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:30 26 4
gpt4 key购买 nike

我是 hibernate 框架的新手。我正在关注此 link 上提供的教程系列.这是我的模型类

package kasun.hibernate.moreAnnotations;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="UserDetailsMoreAnn")
public class UserDetailsMoreAnnotations {
private int id;
private String name;
private String address;
private Date date;
private String desc;

@Id
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
// public String getDesc() {
// return desc;
// }
// public void setDesc(String desc) {
// this.desc = desc;
// }





}

这是我的服务方法

package kasun.hibernate.moreAnnotations;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ServiceMethod {
public static void main(String[] args) {
UserDetailsMoreAnnotations objct= new UserDetailsMoreAnnotations();
objct.setName("Kalanka");
objct.setId(7);
objct.setAddress("Kuliyapitiya");
objct.setDate(new Date()); // provide the current date
// objct.setDesc("Younger Brother");



SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session= sessionFactory.openSession();
session.beginTransaction();
session.save(objct);
session.getTransaction().commit();

}

}

这是我的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License:
GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the
lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->
<!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>

<!-- Database connection settings -->
<!-- <property name="connection.driver_class">org.hsq/ldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> -->

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/HibernateTesting1</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<property name="hbm2ddl.auto">update</property>



<!-- 5) for the kasun.hibernate.moreAnnotations package -->
<mapping class="kasun.hibernate.moreAnnotations.UserDetailsMoreAnnotations" />




</session-factory>

</hibernate-configuration>

以上代码运行没有错误。我遇到的问题是,当我取消注释这些行时

   public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}

在我的 model class 中,通过取消注释行将值设置为 descobjct.setDesc("弟弟");服务方法

我得到一个错误,因为 Unable to execute command [alter table UserDetailsMoreAnn add column desc varchar(255)] 它说 你的 SQL 语法有错误;查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“desc varchar(255)”附近使用的正确语法。我做错了什么?谁能帮我解决这个问题。提前致谢

最佳答案

DESC是MySQL中的保留关键字,不能用于字段名。尝试重命名为 Description 或其他名称。 :)

您也可以注释该字段,但必须使用括号或转义引号:

@Column(name = "[DESC]")
公共(public)字符串 getDesc() {

@Column(name = "\"DESC\"")
公共(public)字符串 getDesc() {

MySQL 5.7 的保留字列表:https://dev.mysql.com/doc/refman/5.7/en/keywords.html

关于java - 为什么这个 String desc 变量没有在 hibernate 中插入到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38308711/

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