0

Working with hibernate and h2 database. I've set hibernate.hbm2ddl.auto to 'create', all works fine, table in db was created. If I comment this code it will work too. But if I need to change schema e.g. add new field I need to set it to 'update, but when I do it I get an error even if I din't change schema and code:

INFO: HHH000262: Table not found: Car
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [create table Car (id bigint not null, color varchar(255), name varchar(255), driver_id bigint, primary key (id))]
Caused by: org.h2.jdbc.JdbcSQLException: Table "CAR" already exists; SQL statement:
create table Car (id bigint not null, color varchar(255), name varchar(255), driver_id bigint, primary key (id)) [42101-188]

Why do I get an error when I change hbm2ddl.auto to 'update' and how to fix it? As I see it can't find table, but it there hibernate created it, and now can't see it, why so?

My code:

public class Service {
private final SessionFactory ourSessionFactory;
private final ServiceRegistry serviceRegistry;

public Service(Class... classes){
    try {
        Configuration configuration = new Configuration()
                .addProperties(ConnectionProperties.getH2(true))
                /*.addAnnotatedClass(Car.class)
                .addAnnotatedClass(Driver.class)
                .addAnnotatedClass(House.class)*/;
        for (Class clas: classes) {
            configuration.addAnnotatedClass(clas);
        }

        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        ourSessionFactory = configuration.buildSessionFactory(serviceRegistry);
    } catch (Throwable ex) {
        throw new ExceptionInInitializerError(ex);
    }
}

public Session getSession() throws HibernateException {
    return ourSessionFactory.openSession();
}
}

public class CarService extends Service {

public CarService() {
    super(Car.class, Driver.class);
}

public List<Car> getAllCars() {
    final Session session = getSession();
    List<Car> cars;
    try {
        session.beginTransaction();
        cars = session.createQuery("from Car").list();
        session.getTransaction().commit();
    } finally {
        session.close();
    }
    return cars;
}

public void addCar(Car car) {
    final Session session = getSession();
    try {
        session.beginTransaction();
        session.save(car);
        session.getTransaction().commit();
    } finally {
        session.close();
    }
}
}

@Entity
public class Car {
@Id
@GeneratedValue
private long id;
private String color;
private String name;
@OneToOne(cascade = javax.persistence.CascadeType.ALL)
private Driver driver;
//getters and setters
}
Max Husiv
  • 305
  • 1
  • 4
  • 12
  • Did you use H2 Console to see if there is a table called Car? Also try to map the "Car"-objects within the hibernate class: Configuration conf = new Configuration(): conf.addAnnotatedClass(Car.class); If this doesn't help, please provide the full hibernate config file (or configuration within class) and the Car class. – marc3l Sep 14 '15 at 13:40
  • It works perfect with mysql. – Max Husiv Sep 14 '15 at 13:50
  • If I drop tables and run it with 'update' option I get an error:INFO: HHH000262: Table not found: Car Exception in thread "main" java.lang.ExceptionInInitializerError – Max Husiv Sep 14 '15 at 13:51
  • Marcel, yes I'm using H2 console, and there is tables called Car. – Max Husiv Sep 14 '15 at 13:54

0 Answers0