Reputation: 147
I have a problem with my project. When i browse http://localhost:8080/user/form
to fill information user
Then submit form to save user and automaticlly direct http://localhost:8080/user/list
to display list User, but occur following error:
Hibernate: insert into userdat (age, birthday, gender, password, username) value s (?, ?, ?, ?, ?) Mar 27, 2016 8:30:17 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$Standar dWarningHandler logWarning WARN: SQL Warning Code: 10000, SQLState: 01J01 Mar 27, 2016 8:30:17 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$Standar dWarningHandler logWarning WARN: Database 'D:\PROJECTSPRING\userdb' not created, connection made to existin g database instead. Mar 27, 2016 8:30:17 AM org.hibernate.hql.internal.QueryTranslatorFactoryInitiat or initiateService INFO: HHH000397: Using ASTQueryTranslatorFactory Hibernate: select user0_.username as username1_0_, user0_.age as age2_0_, user0_ .birthday as birthday3_0_, user0_.gender as gender4_0_, user0_.password as passw ord5_0_ from userdat user0_ Mar 27, 2016 8:30:17 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExce ptions WARN: SQL Error: 20000, SQLState: 22018 Mar 27, 2016 8:30:17 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExce ptions ERROR: Invalid character string format for type int. [WARNING] org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.DataException: could not execute que ry
Caused by: org.hibernate.exception.DataException: could not execute query at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQL ExceptionTypeDelegate.java:52) at org.hibernate.exception.internal.StandardSQLExceptionConverter.conver t(StandardSQLExceptionConverter.java:42) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlException Helper.java:111)
Caused by: java.sql.SQLDataException: Invalid character string format for type i nt. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknow n Source) at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source ) at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException (Unknown Source)
Here file UserDaoImpl.java
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
public LocalSessionFactoryBean sessionFactory;
@Override
public void save(User user) {
// TODO Auto-generated method stub
Session session = sessionFactory.getObject().openSession();
session.save(user);
session.flush();
session.close();
}
@Override
public void update(User user) {
// TODO Auto-generated method stub
}
@Override
public List<User> listUsers() {
// TODO Auto-generated method stub
Session session = sessionFactory.getObject().openSession();
Query query = session.createQuery("from User");
return (List<User>)query.list();
}
Here file User.java
@Entity
@Table(name = "userdat",uniqueConstraints={@UniqueConstraint(columnNames="username")})
public class User {
@Column(name = "gender", nullable = false)
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@Id
@Column(name = "username", unique = true, nullable = false)
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "password", nullable = false)
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
@Column(name = "birthday", nullable = false)
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
@Column(name="age", nullable = false)
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
private String userName;
private String passWord;
private Date birthDay;
private Integer age;
private Gender gender;
}
Here file Gender.java
public enum Gender {
MALE("Male"),
FEMALE("Female"),
OTHER("Other");
private String name;
private Gender(String name) {
this.name = name;
}
public String getGender(){
return this.name;
}
}
Here Create Table in DB
public void createTable() throws SQLException{
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection connection = DriverManager.getConnection("jdbc:derby:D:/PROJECTSPRING/dbuser;create=true");
createTableNotExist(connection,"userdat", "create table userdat"
+ "(username varchar(1000) primary key,"
+ "password varchar(1000),birthday date,"
+ "age integer,gender varchar(100))");
// createTableNotExist(connection,"subject","create table subject"
// + "(id bigint primary key generated always as identity(start with 1,increment by 1),"
// + "title varchar(1000),student integer,score integer)" );
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 0
Views: 4869
Reputation: 304
I'm publishing this answer because this error can be also caused by using column names in the CSV file.
Here is the thing: I extracted some data from a DB in CSV format using an automation tool. Then I tried to import data to Apache Derby without any success. After a couple of hours finally I figured out that column names should have been removed from the file.
It's a very simple solution but sometimes we just want run into the process ASAP and forget the details about process configuration.
I hope this post help others.
Cheers!
Upvotes: 0
Reputation: 83
Enums persist by ordinal number if they do not annotated with @Enumerated
. In this case hibernate expects int but gets varchar(100). Code below solves the problem.
@Column(name = "gender", nullable = false)
@Enumerated(EnumType.STRING)
Upvotes: 1
Reputation: 11551
I think it is saying that it wants to store Gender
as in Integer since it is an enum type. Change the database creation to:
gender integer
Upvotes: 0