Kasun Siyambalapitiya Kasun Siyambalapitiya - 4 months ago 11
MySQL Question

why this String desc variable not get inserted to the database in hibernate

I am a newbie to

hibernate
framework. I following a tutorial series available on the this link. Here is my
model class


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;
// }





}


and this is my
Service method


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();

}

}


and this is my
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>


Above code runs without errors. The problem I encounter is that when I uncomment the lines

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


in my
model class
and set a value to
desc
by uncommenting the line
objct.setDesc("Younger Brother");
in the
Service Method


I got a error as
Unable to execute command [alter table UserDetailsMoreAnn add column desc varchar(255)]
and it says that
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc varchar(255)' at line 1
. What I am doing wrong? Can anyone help me to fix this.Thanks in advance

Answer

DESC is a reserved keyword in MySQL and cannot be used for field name. Try renaming to Description or something. :)

list of reserved words for MySQL 5.7: https://dev.mysql.com/doc/refman/5.7/en/keywords.html