0

I have a problem with my project. When i browse http://localhost:8080/user/form to fill information user

enter image description here

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();
        }
6
  • Try using Embeddable annotation on Gender class and with Embedded annotation on Gender field in User class. Refer to link to see the usage. Commented Mar 27, 2016 at 9:16
  • @MadhusudanaReddySunnapu, i have try with your way, however when i mvn jetty:run to run server but occur error: Commented Mar 27, 2016 at 9:46
  • Caused by: org.springframework.beans.factory.BeanCreationException: Could not au towire field: private edu.java.spring.service.user.dao.UserDao edu.java.spring.s ervice.user.controller.UserRestServiceController.userDao; nested exception is or g.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDaoImpl': Injection of autowired dependencies failed; nested exception Commented Mar 27, 2016 at 9:46
  • is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.orm.hibernate4.LocalSessionFactoryBean edu.jav a.spring.service.user.dao.impl.UserDaoImpl.sessionFactory; nested exception is o rg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/userservice- servlet.xml]: Invocation of init method failed; nested exception is org.hibernat e.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tupl e.component.PojoComponentTuplizer] Commented Mar 27, 2016 at 9:46
  • @MadhusudanaReddySunnapu, you have another way ???? Commented Mar 27, 2016 at 9:57

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.