java - why this String desc variable not get inserted to the database in hibernate -
i newbie hibernate
framework. following tutorial series available on link. here 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 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 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 hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!-- ~ hibernate, relational persistence idiomatic java ~ ~ license: gnu lesser general public license (lgpl), version 2.1 or later. ~ see lgpl.txt file in 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 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 executed sql stdout --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <!-- 5) kasun.hibernate.moreannotations package --> <mapping class="kasun.hibernate.moreannotations.userdetailsmoreannotations" /> </session-factory> </hibernate-configuration>
above code runs without errors. problem encounter when uncomment lines
public string getdesc() { return desc; } public void setdesc(string desc) { this.desc = desc; }
in model class
, set value desc
uncommenting line objct.setdesc("younger brother");
in service method
i got error unable execute command [alter table userdetailsmoreann add column desc varchar(255)]
, says you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'desc varchar(255)' @ line 1
. doing wrong? can me fix this.thanks in advance
desc
reserved keyword in mysql , cannot used field name. try renaming description
or something. :)
you can annotate field, must use brackets or escaped quotes:
@column(name = "[desc]") public string getdesc() {
or
@column(name = "\"desc\"") public string getdesc() {
list of reserved words mysql 5.7: https://dev.mysql.com/doc/refman/5.7/en/keywords.html
Comments
Post a Comment