In my spring project, i am using Hibernate to export my entity classes to a previously created database. But this will require the final user knows how to create a database in the Database manager system (Currently I am using Postgresql).
Is there any way of, given only the machine where the postgresql is installed (and the username and password, which is provided when the application is runned the first time), the Hibernate create a new database in the server if it doesn't exist?
If your configuration looks like this
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://host:port/database</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Then the database will be created by Hibernate automatically.
Update:
Ok now I understand what you want. You want to start the Postgresql server with Hibernate. This is not possible. Hibernate does not do this.
You can do this with
Another script that starts with your application
A maven/ant target.
A build job
But the best solution is to use an in-memory database that does not need an external server (for example H2, or Java derby)
See also
Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?
and
Postgres database create if not exists
Take a look of paramater hibernate.hbm2ddl.auto for your hibernate.cfg.xml file. I suggest you this link: Hibernate hbm2ddl.auto, possible values and what they do - any official explanation?
Run "CREATE DATABASE ..." (see http://www.postgresql.org/docs/9.0/static/sql-createdatabase.html) as a native SQL query ...
.createSQLQuery(" ... ").executeUpdate(); ...
Hibernate will - at least as far as I know - not create the database, only the tables in it.
I suppose you need to connect to postgresql via a second persistence unit/connection, because of the chicken-and-egg nature of this approach.
Related
I'm using hibernate for my web application and it's working fine. I have set the properties of connection pooling like below.
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
I have set min_size = 5, max_size=20, max_statements=50
but it could be min_size=1, max_size=100, max_statements=500
so, at what basis should I set these values? I have read some tutorials
about hibernate connection pooling but didm't get any specific idea
how to set these properties' values
that totally depends on how much load your application have, to check how much db activities are happening on c3p0 side you have to monitor the internal stats of objects and you need JMX to see that stats and based on that you can manage and configure pool, plz check below link
Configuring and Managing c3p0 via JMX
I would also recommend you to Check HikariCP, as its way much better than c3p0.
I have a glassfish container managed derby database that I can access using CRUD operations. I would like to access my derby database directly through the asadmin tool to view the tables.
However, I cannot find my database.
After researching this site i see that glassfish creates connection pools that connect only when required. I see that it is possible for me to create a connection pool but I don't even know where the database is.
Any advice is greatly appreciated. I am new to JEE7 and learning from 'JEE7 for beginners' book.
<persistence-unit name="chapter15PU" transaction-type="JTA">
<jta-data-source>jdbc/__default</jta-data-source>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and- create"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
To connect using derby ij command line tool use the following command:
connect 'jdbc:derby://localhost:1527/sun-appserv-samples';
'sun-appserv-samples' is the default Glassfish container managed db name.
As follows;
Oracle documentation on domain.xml file
If you specify that you're database is to be container managed and you are using Glassfish, then all of the database properties can be found in the domain.xml file.
For example, if you are using the default glassfish domain named domain1, then you must navigate to ..glassfish/domains/domain1 on you're file system. The domain.xml file can be found in the config folder.
Here you will find the following information detailing all of you're database properties.
<property name="PortNumber" value="1527"></property>
<property name="Password" value="APP"></property>
<property name="User" value="APP"></property>
<property name="serverName" value="localhost"></property>
<property name="DatabaseName" value="sun-appserv-samples"></property>
<property name="connectionAttributes" value=";create=true"></property>
As you can see the default database name is sun-appserv-samples.
To connect to this database you must open the command tool ij which can be found in you're derby bin folder. Then use the following command to connect to the database.
connect 'jdbc:derby://localhost:1527/sun-appserv-samples';
Note: You must have derby running and you're java web application deployed on Glassfish first.
I am not sure you can access directly a derby database that is in-memory.
I am in the process of learning JPA - Hibernate.
I am following this article
In Dog.java it is mentioned as #Table(name = "dog").
In persistence.xml I have the following
<property name="hibernate.hbm2ddl.auto" value="create"/>
Does this creates table dog in database? I have not created table Dog in database. So in production environment this could be dangerous though. In such scenarios what should be ideal value for hibernate.hbm2ddl.auto?
Any suggestions?
it's dangerous in all senses, your application user should not have DDL permissions (alter table, create tables) your application user should only do DML (SELECT, INSERT,UPDATE,DELETE, etc)
Yes, it does create the new table every time that your app is deployed. Better to use:
<property name="hibernate.hbm2ddl.auto" value="validate"/>
if you already have data in place.
Here are the possible options:
validate: validate the schema, makes no changes to the database.
update: update the schema.
create: creates the schema, destroying previous data(!)
create-drop: drop the schema at the end of the session(!)
Do not set <property name="hibernate.hbm2ddl.auto" value="create"/> in production, because whenever you restart the server, all tables will be deleted and newly created again. You can make use of this property(hibernate feature) if you are migrating from one database to another.
If you want to set then set <property name="hibernate.hbm2ddl.auto" value="update"/> in development(not in production). This will update the schema if there are any changes you have made in pojo classes(annotations).
Also check : Hibernate: hbm2ddl.auto=update in production?
Hibernate hbm2ddl.auto possible values and what they do?
Set it to "none" in a production environment.
How can I debug a query built with JPA 2.0 CriteriaBuilder? Is there a way to print out the query that is being executed?
I am developing a web application using NetBeans, MySql, GlassFish. I would avoid starting MySql in debug mode, because it is used also for other applications. JPA provider is EclipseLink.
The same attributes in persistence.xml that also print the SQL that is generated from regular JPQL queries, should also print the SQL that is generated from Criteria queries.
E.g.
For Hibernate (used by e.g. JBoss AS) it's:
<property name="hibernate.show_sql" value="true" />
For EclipseLink (used by e.g. GlassFish) it's:
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
Also see: How to view the SQL queries issued by JPA?
If you are using Java and Spring, under resources, you can adjust your application.properties file and add the line below:
spring.jpa.show-sql=true
1)When i send my application to my teacher, how do i tell it to create some empty tables and populate it with sample data on FIRST launch of application? I know that i can include sql script with my files or something. Can i instruct hibrenate to create empty tables? I have my entities which i use in my application.
2)When my ide (NetNeans) starts, i go to services and start my db (JavaDB) manually, because if i dont, on application startup hibernate complains that he cant connect. How do i tell hibernate start to start db (its on localhost:1527)?
my hebernate.cfg:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property>
<property name="hibernate.connection.username">app</property>
<property name="hibernate.connection.password">app</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<mapping resource="entity/PurchaseOrder.hbm.xml"/>
<mapping resource="entity/Customer.hbm.xml"/>
<mapping resource="entity/ProductCode.hbm.xml"/>
<mapping resource="entity/Product.hbm.xml"/>
<mapping resource="entity/MicroMarket.hbm.xml"/>
<mapping resource="entity/Manufacturer.hbm.xml"/>
<mapping resource="entity/DiscountCode.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I dont have good knoledge in english and its hard to form query in google. Just tell me how those functions called (if they exsist).
Any help would be appreciated. Thanks in advance.
1)When i send my application to my teacher, how do i tell it to create
some empty tables and populate it with sample data on FIRST launch of
application? I know that i can include sql script with my files or
something. Can i instruct hibrenate to create empty tables? I have my
entities which i use in my application.
Yes you can using hbm2ddl property, See this
When my ide (NetNeans) starts, i go to services and start my db (JavaDB) manually, because if i dont, on application startup hibernate complains that he cant connect. How do i tell hibernate start to start db (its on localhost:1527)?
It isn't good thing to handle such things by our app, instead make your app to accept different configurations using properties file may be