Hibernate adds custom prefix to directories

I am using Hibernate 4.3.6 and I need to add a custom prefix to my directories depending on the environment. I used this code in version 4.2.3

private static SessionFactory buildSessionFactory() {
    try {
        Configuration config = new Configuration();
        config.configure("db.cfg.xml");
        config.buildMappings();

        ServiceRegistry registry = new StandardServiceRegistryBuilder()
                .applySettings(config.getProperties())
                .build();

        if (prefix != null && !prefix.isEmpty()) {
            Iterator<Table> iterator = config.getTableMappings();
            while (iterator.hasNext()) {
                Table table = (Table) iterator.next();
                table.setCatalog(prefix + table.getCatalog());
            }
        }
        //return new AnnotationConfiguration().buildSessionFactory(registry);
        SessionFactory factory = config.buildSessionFactory(registry);
        return factory;
    }
    catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println(ex);
        throw new ExceptionInInitializerError(ex);
    }
}

      

But now he's wrong on

config.buildSessionFactory(registry);

java.lang.ExceptionInInitializerError
at db.DatabaseEngine.buildSessionFactory(DatabaseEngine.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1456)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at db.DatabaseEngine.buildSessionFactory(DatabaseEngine.java:104)
... 26 more

      

Is there such a thing as ImprovedNamingStrategy but for directories?

+3


source to share


3 answers


After creating the orm.xml, you can load it with the following code:

Configuration config = new Configuration();
config.configure("db.cfg.xml");
config.addResource("orm.xml"); // Load these files in any order
config.buildMappings();

      



See Vlad's answer on how to create an orm.xml file.

+1


source


According to Hibernate Annotation docs , XML orm.xml options can override annotation configuration:

<entity-mappings 
  xmlns="http://java.sun.com/xml/ns/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
  version="2.0">

    <persistence-unit-metadata>
        <persistence-unit-defaults>
            <schema>${db.schema}</schema>
            <catalog>${db.catalog}</catalog>
        </persistence-unit-defaults>
    </persistence-unit-metadata>

    <entity class="package.YourEntity1">        
        <table name="YOUR_TABLE_1" catalog="${specific_catalog1}"/>       
    </entity>

    <entity class="package.YourEntity2">        
        <table name="YOUR_TABLE_2" catalog="${specific_catalog2}"/>       
    </entity>

      



Schema / directory variables can be overridden by Maven based on a specific profile that you run prior to creation. This works for both the default directory and specific entity directory definitions.

+1


source


It won't be easier to load the config file into the environment, I mean (depends on env)

config.configure("db.cfg-env1.xml");

      

or

config.configure("db.cfg-env2.xml");

      

and set the correct directory in the jdbc connection url?

An example is close to implementation (thanks to serge for letting me know :))

config.configure(String.format("db.cfg-%s.xml"), env);

      

0


source







All Articles