0

What is the difference in Hibernate of
cfg.setProperty(Environment.DEFAULT_SCHEMA, "exampleDB");

with
<property name="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 of Environment.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?

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
Cratylus
  • 52,998
  • 69
  • 209
  • 339

3 Answers3

2

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.

James DW
  • 1,815
  • 16
  • 21
1

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()

HRgiger
  • 2,750
  • 26
  • 37
  • Ok, what but does the `Environment.DEFAULT_SCHEMA` represent? – Cratylus Sep 17 '11 at 08:17
  • what I understand2: giving you read/write access hibernate.cfg default_schema property at runtime. Lets say you write an application and want to check if a condition is true then you want to set default_schema as a, if false you want to set as b. Well I am learning with you as well, so hope it helps:) – HRgiger Sep 17 '11 at 09:00
1

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)

Community
  • 1
  • 1
meriton
  • 68,356
  • 14
  • 108
  • 175