Hibernate error, possibly with DTD declaration

前端 未结 2 946
名媛妹妹
名媛妹妹 2020-12-06 19:32

Our project use\'s Hibernate\'s programmatic Configuration to set up our SessionFactory and such. I just migrated us from version 3 to version 4 of Hibernate. N

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 20:05

    I also just migrated from 3.0 to 4.0, I assume 3 causes I use the following DTD's

    THE ACTUAL FIX IN THIS CASE

    Make sure that you dont have any old 3.0 jar's in the path, else you can see this exception.

    Possible Cause 1

    For hibernate.cfg.xml

    
     
    

    And for the hbm files

    
    
    

    Works well for me.

    Possible Cause 2

    You have misspelt in your hbm file.

    Edit :

    I am using mixed configuration both programmatic and cfg files. When I tried to use all programmatic, it did not work for me. Nor did I get much help from SO. But the below worked for me.

    try {
        String connection = "jdbc:mysql://"
                + Globals.DBSERVER.trim()
                + "/myDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
        log.debug("Connection URL "+connection) ;
        Configuration configuration = new Configuration();
        configuration
                .setProperty("hibernate.connection.url", connection)
                .setProperty("hibernate.connection.username", Globals.DB_USER_NAME.trim())
                .setProperty("hibernate.connection.password", Globals.DB_PASSWORD.trim())
            ;
        configuration.configure();
            sessionFactory = configuration
                .buildSessionFactory(new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).buildServiceRegistry());
    
                    } catch (Exception e) {
                        log.fatal("Unable to create SessionFactory for Hibernate");
                        log.fatal(e.getMessage());
                        log.fatal(e);
                        e.printStackTrace();
                    }
    

    My question that helped me fix it.

    Overall Advice

    Going all programmatic is a bad idea. Since there is a lot of programmatic stuff you need to add from column to variable mapping to variable type. It will be a debugging nightmare. I suggest doing non programmatic stuff for things that you can do without programmatic. For me I just needed to get the username password from cmd line, so that I can deploy the product on any server. So I just made that programmatic.

提交回复
热议问题