Hi Everyone,
I am new to hibernate.I created program for insert data into MySQL through Hibernate its working.Now i face the problem while i am using in my table the id as primary key and auto increment.Now i need to retrieve the data by passing through the name.But it gives an error as,
Quote:
Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.Integer, got class java.lang.String
I dont know the id because i am using auto increment.Please let me know how to retrieve data without load id(primary key).
Here I attached the db structure and my hbm.xml file.
The db file management likes below.
Code:
CREATE TABLE `file management` (
`File Id` int(11) NOT NULL AUTO_INCREMENT,
`File Name` varchar(45) DEFAULT NULL,
`File Type` varchar(45) DEFAULT NULL,
`File Size` varchar(45) DEFAULT NULL,
PRIMARY KEY (`File Id`),
KEY `File Name` (`File Name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1$$
Here i used the code for retrieve data.
Code:
session.get(FileManagement,"fileName");
transaction=session.beginTransaction();
List<?> groupName = session.createQuery("from FileManagement").list();
String fileType = null;
for (int i = 0; i < list.size(); i++) {
FileManagement fileManagement = (FileManagement) list.get(i);
if(fileManagement.getFileName().equals(fileName)){
fileType = fileManagement.getFileType();
}
}
Here the code for FileManagement class
Code:
public class FileManagement implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer fileId;
private String fileName;
private String fileType;
private String fileSize;
public FileManagement() {
}
public FileManagement(String fileName, String fileType, String fileSize) {
this.fileName = fileName;
this.fileType = fileType;
this.fileSize = fileSize;
}
public Integer getFileId() {
return this.fileId;
}
public void setFileId(Integer fileId) {
this.fileId = fileId;
}
public String getFileName() {
return this.fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileType() {
return this.fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public String getFileSize() {
return this.fileSize;
}
public void setFileSize(String fileSize) {
this.fileSize = fileSize;
}
}
The FileManagement.hbm.xml file
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 18, 2014 4:41:00 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="fs.mysql.hibernate.FileManagement" table="`file management`" catalog="`portal`">
<id name="fileId" type="java.lang.Integer">
<column name="`File Id`" />
<generator class="identity" />
</id>
<property name="fileName" type="string">
<column name="`File Name`" length="45" />
</property>
<property name="fileType" type="string">
<column name="`File Type`" length="45" />
</property>
<property name="fileSize" type="string">
<column name="`File Size`" length="45" />
</property>
</class>
</hibernate-mapping>
Let me know,how to retrieve data without using id(primary key).This table contain primary key and auto increment for Id.
Thanks,
Ashok