I am storing an Object into my database, and I think I am making some stupid mistake which I don’t see. I attached some code to understand my problem:
MyObject.java object which I want to store in the database
@Entity
@Table(name = "my_object")
public class MyObject {
private String idName;
@Id
@Column(name = "id", unique = true, length = 50,nullable=false)
public String getIdName() {
return idName;
}
public void setIdName(String idName) {
this.idName = idName;
this.id = idName.hashCode();
}
TestToStoreObject.java
MyObject obj = new MyObject();
obj.setIdName("id");
//More set ...
save(obj);
Function to store the Object into the database
protected void save(MyObject obj) throws DataAccessLayerException {
try{
getHibernateTemplate().save(obj);
} catch (HibernateException e) {
handleException(e);
}
}
This is a general function which is working with other datatypes. So where I guess the problem is, is in configuration files. The exception is:
org.springframework.dao.InvalidDataAccessResourceUsageException:
could not insert: [path.MyObject]; SQL [insert into object (id) values (?)];
nested exception is org.hibernate.exception.SQLGrammarException:
could not insert: [path.MyObject]
Here more configuration files:
HibernatePlatform.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="path.User" />
<mapping class="path.MyObject" />
The problem was that I had another class with the same name in another package, and it was messing. A piece of advise for the future: chach this small things!!