What is the difference in Hibernate of
cfg.setProperty(Environment.DEFAULT_SCHEMA, "exampleDB");
with
<property na开发者_开发技巧me="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/exampleDB</property>
Is default schema and DB in the connection URL completely different things?
I noticed that if I omit the setting ofEnvironment.DEFAULT_SCHEMA
and do the following:
Configuration cfg = new Configuration();
System.out.println("Default schema:"+cfg.getProperty(Environment.DEFAULT_SCHEMA));
sessionFactory = cfg.configure().buildSessionFactory();
The console prints: Default schema:null
.
So what is the use of Environment.DEFAULT_SCHEMA
and the difference with the connecting DB?
The default schema property is used when your DBA creates multiple schemas within a single database. I've seen this with postgres where everyone uses one database URL but under that there are separate schemas for each application.
By using the default schema property it allows you to keep the schema names off your entities. This is particularly useful if you're running tests against HSqlDB which does not support schemas and you deploy against a DB that is using schemas. Having a null value just means it defaults back to the DB default schema.
refer to: http://www.youtube.com/watch?v=ReAZmA83Myg&feature=related and http://www.java-forums.org/database/1467-variables-hibernate-cfg-xml-file.html
what I understand: in hibernate.cfg.xml you can setup;
<property name="hibernate.default_schema">exampleDB</property>
or you can construct the hibernate.cfg file at execution time;
cfg.setProperty(Environment.DEFAULT_SCHEMA, "exampleDB");
how you can create default schema for later use;
new SchemaExport(config).create(true,true); //First parameter (true) creates new schema
edit: and another reference: http://docs.jboss.org/hibernate/core/3.3/api/org/hibernate/cfg/Environment.html
Provides access to configuration info passed in Properties objects.
Hibernate has two property scopes:
Factory-level properties may be passed to the SessionFactory when it instantiated. Each instance might have different property values. If no properties are specified, the factory calls Environment.getProperties().
System-level properties are shared by all factory instances and are always determined by the Environment properties.
The only system-level properties are
hibernate.jdbc.use_streams_for_binary
hibernate.cglib.use_reflection_optimizer
Environment properties are populated by calling System.getProperties() and then from a resource named /hibernate.properties if it exists. System properties override properties specified in hibernate.properties.
The SessionFactory is controlled by the following properties. Properties may be either be System properties, properties defined in a resource named /hibernate.properties or an instance of java.util.Properties passed to Configuration.buildSessionFactory()
Setting the hibernate property causes hibernate to qualify all table names in the sql it emits, i.e. instead of
select count(*)
from mytable
it would send
select count(*)
from myschema.mytable
to the database.
The effect of appending to the connection string is database specific. Some databases (e.g. Oracle) do not support specifying the default schema in the connection string. (source)
精彩评论