Asked  6 Months ago    Answers:  5   Viewed   56 times

Is it okay to run Hibernate applications configured with hbm2ddl.auto=update to update the database schema in a production environment?

 Answers

55

No, it's unsafe.

Despite the best efforts of the Hibernate team, you simply cannot rely on automatic updates in production. Write your own patches, review them with DBA, test them, then apply them manually.

Theoretically, if hbm2ddl update worked in development, it should work in production too. But in reality, it's not always the case.

Even if it worked OK, it may be sub-optimal. DBAs are paid that much for a reason.

Tuesday, June 1, 2021
 
KHM
answered 6 Months ago
KHM
33

i tried this below code from link and it seem to work beautifully i will paste the code in-case the link were broken in future.

Keep it Simple Keep it Smile :)

    /**
    * An utility method to build the Criterion Query IN clause if the number of parameter
    * values passed has a size more than 1000. Oracle does not allow more than
    * 1000 parameter values in a IN clause. Doing so a {@link SQLException} is
    * thrown with error code, 'ORA-01795: maximum number of expressions in a list is 1000'.
    * @param propertyName
    * @param values
    * @return
    */
import java.util.List;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author 2796
 */
public class SplitHibernateIn {

    private static int PARAMETER_LIMIT = 999;

    public static org.hibernate.criterion.Criterion buildInCriterion(String propertyName, List values) {
        org.hibernate.criterion.Criterion criterion = null;

        int listSize = values.size();
        for (int i = 0; i < listSize; i += PARAMETER_LIMIT) {
            List subList;
            if (listSize > i + PARAMETER_LIMIT) {
                subList = values.subList(i, (i + PARAMETER_LIMIT));
            } else {
                subList = values.subList(i, listSize);
            }
            if (criterion != null) {
                criterion = Restrictions.or(criterion, Restrictions.in(propertyName, subList));
            } else {
                criterion = Restrictions.in(propertyName, subList);
            }
        }
        return criterion;
    }
}
Sunday, August 1, 2021
 
dimi
answered 4 Months ago
77

I'm concerned this will hit the same problem and I'm interested in finding more documentation about how this validation actually works.

In my opinion, the best documentation is the source code that you could check to see what is happening exactly. The relevant method is org.hibernate.tool.hbm2ddl.SchemaValidator#validate().

I went quickly through the code and I don't think that the SchemaValidator verifies foreign keys in the database: it checks the presence of tables, columns, id generators but not foreign keys. A test against a pet database seems to confirm this behavior: dropping a FK constraint doesn't break schema validation (in other words, the validator checks if the application can run, not for referential integrity).

Now, HHH-3532 being marked as fixed, why don't you upgrade to a newer version of Hibernate or, if changing the version of Hibernate is too heavy, why don't you apply the patch for HHH-3532 yourself?

Having that all said, I don't use hibernate.hbm2ddl.auto=update to update production databases, I use change scripts. But I use hibernate.hbm2ddl.auto=validate and I'm happy with it.

Tuesday, August 3, 2021
 
previous_developer
answered 4 Months ago
16

There are several issues in JIRA about this (HHH-2018, HHH-1872, and HHH-1329) so this might clearly be a new one. My understanding is that the easiest workaround for now is to turn hbm2ddl off. If you think that your case is not covered by the existing issues (e.g. because you are using MySQL), maybe open a new one.

Sunday, October 24, 2021
 
Some programmer dude
answered 1 Month ago
13

NHibernate Sets don't have "order" even though the set mapping supports an order-by. See Ayende's explanation:

Note that [order-by] does not work with generic sets and that in general, you don’t want to rely on those ordering properties, you want to use the natural properties of the selected collection. Sets are, by definition, unordered set of unique elements.

You can append the order by clause to the underlying SQL, but that order is lost when NHibernate builds the set. Using an "ordered" set or other collection type (bag or list) will work, of course.

Thursday, October 28, 2021
 
Andrew
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share