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
Related
I developed a gwt application and I used Hibernate as database and it works very well.
Now when I put the application on my phone (format .apk) and I change ip address of my database (mysql) in hibernate file configuration, it gives me an error (ALERT Error).
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.1.218/MyProject_hibernate</property>
<property name="hibernate.connection.username">ns</property>
<property name="hibernate.connection.password">ns</property>
I don't know if this approach is logical or not know (to change only the IP address), if you have any idea please share it with us.
Thank you in advance
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.
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.
I am implementing jpa persistence using hiberante-entity manager in a java web project.
I have set following property in in persistence.xml.
<property name="hibernate.hbm2ddl.auto" value="update"/>
I have a schema for each user. For e.g i have a schema for user1 and one for user2.
If the table 'ABC' is present in user1 schema but not in user2 schema & I deploy the application and it uses user2 db credentials, i get the message 'user1.ABC' table found so the 'ABC' table is not created in user2 schema.
When i tried with following property in the persistence.xml file the table is created in the user2 schema.
<property name="hibernate.hbm2ddl.auto" value="create"/>
My question is
why hibernate is searching in another schema i.e user1 if the application is using user2 db credentials? and
I don't want to create the schema every time the server is started so how can i avoid using
the value 'create'.
EDIT: Below is my persistence.xml file
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="XXXXXX" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.axonframework.saga.repository.jpa.SagaEntry</class>
<class>org.axonframework.saga.repository.jpa.AssociationValueEntry</class>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Thanks in advance
I am also facing the same issue, and after digging a lot, get to know that the bug is related to the Mysql Connector.
After changing MySql Connector 6.0.5 to 5.1.28 It works fine for me.
I hope It can help you. Cheers
Has the same problem.
After set <property name="hibernate.default_schema" value="MY_SCHEMA"/> the problem has been solved.
Check if you are calling user1 and user2 in your Hibernate Sessionfactory.
If you want to handle several schemas properly then use multi-tenant per schema also if you want to update/create/migrate/handle columns/tables/schemas/databases then use flyway or liquibase
REFERENCES
Multitenancy https://vladmihalcea.com/hibernate-database-schema-multitenancy/
Flyway https://flywaydb.org
Liquibase https://www.liquibase.org
The Hibernate documentation is clear about this, you need to enable multi-tenant operations as described in this answer and this example.
Basically you have to declare multiple persistence units and have each point to a different schema. Each can then use different login credentials as well.
Hibernate documentation link
To summarise:
Define your persistence unit
Define your mapping files per persistence unit
When using JPA add the following:
3. Specifying tenant identifier from SessionFactory
4. Implement a MultiTenantConnectionProvider
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.