How to automatically map POJOs to database to create tables using NetBeans - java

I have wrote some POJOs in NetBeans, and want to map these entities automatically to an empty database, to be tables.
I have read the netbeans official tutorial https://netbeans.org/kb/docs/java/hibernate-java-se.html#06a
But using Hibernate Mapping File as the document says can not choose the Database Table value, compare to the pic
(https://netbeans.org/images_www/articles/70/java/hibernate-j2se/mapping-wizard.png,
The actor value will not show since my database is empty.
So what should I do if I followed the tutorial, or is there any other method to automatically create tables by POJO in NetBeans?

There is one property hibernate.hbm2ddl.auto in hibernate which create tables as per your pojo structure.
Refer this doc.

MyBatis Generator can generate POJOs of tables. Visit this site to help you get started http://code.google.com/p/mybatis/wiki/Generator

Follow this tutorial on Hibernate mapping files.
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/xml.html
I never used NetBeans but if you write mapping file by yourself then you can use an empty database.

Use SchemaExport.export. Run following code in main() method:
AnnotationConfiguration configuration = new AnnotationConfiguration();
SchemaExport schemaExport = new SchemaExport(configuration);
schemaExport.export(true, true, true, false);

Related

Embedded database and multiple catalogs

I want to create integration tests for my repositories. The production database is Sybase and it consists of multiple catalogs in which there are multiple schemas.
In my code I use multiple queries I am selecting data across different catalogs: ex:
select *
from catalog_a.schema_a.table_1 aa1, catalog_b.schema_a.table_2 ba2
where aa1.c1 = ba2.c2
So for the tests I would like to create embedded database, like H2, HSQLDB or something different. I was trying to find something that would allow me to simulate prod db with multiple catalogs, but I couldn't make it work. Please advice and suggest the solution.
I am writing app in java/spring. Additional trick here is that my app is creating only one DataSource to database.
HSQLDB supports only a single catalog and the name is checked when the catalog is specified in a query. You can change the catalog name from the default PUBLIC to something else. For example:
ALTER CATALOG public RENAME TO to catalog_a
But using two different catalog names is not supported.
If your schema or table names in the two catalogs are different, you could modify the source code of HSQLDB and disable the catalog name check for your tests in the method org.hsqldb.ParserDQL.checkValidCatalogName(String name)
I managed to achieve this in H2 via IGNORE_CATALOGS property and version 1.4.200.
However, the url example from their docs did not seem to work for me, so I added a statement in my schema.xml:
SET IGNORE_CATALOGS = true;

Apache ibatis postgresql selectList how it it works

I have installed postgresql enterprise DB and created some tables
In one of the example. I see the following code.
import org.apache.ibatis.session.SqlSession;
.....
SqlSession session = sessionFactory.openSession();
.....
List list = session.selectList("findAllData-Data", params);
What it does? findAllData-Data means what? I have only created tables in postgresql, but I don't see the table name in above code
Check up the documentation for selectList
"findAllData-Data" is the unique identifier matching the statement to use. Statements are usually defined in some mapper .xml file or in newer versions in annotation of a class, so grep your code for findAllData-Data in order to find the definition.

Creating a new Hibernate table

So I'm still pretty new to Hibernate, and I'm working on a large-ish application that already has a database with several Hibernate tables. I'm working on a new feature, which includes a new #Entity class, and I need these objects to be stored in a new table. The class is declared like this:
#Entity
#Table(name="DATA_REQUEST")
public class DataRequest {
//Some fields, nothing fancy
}
The DATA_REQUEST table does not exist, nor do I have any data to store in it yet. I started the application up, expecting that it would either create the table or crash because it doesn't exist yet. Neither of these actually happened.
So: do I need to create the table manually (easily done)? Or do I need to go somewhere else to tell Hibernate that I need this table? I've seen the hibernate.cfg.xml file, which looks like a good place to start.
You need to specify "create" for the "hibernate.hbm2ddl.auto" property. Read more details here. This is not recommended in production but only for testing purposes.
As for adding a new column to the table
As long as it is not a not null column you don't need drop the table or restart your hibernate app
If you do want to use the column then you need to map the column in the code/hbm file, so you will have to restart the hibernate app
If there is no mapping present as far as hibernate is concerned the column does not exisist, If it is a not null column then underlying data base would reject inserts/updates as hibernate will not include the column in generated sql
from hibernate documentation
hibernate.hbm2ddl.auto
Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.
e.g. validate | update | create | create-drop
hibernate Configuration

Use SQL Information to Populate Schema Documentation and Code Comments Using Hibernate Tools?

I have set up Hibernate Tools from within Eclipse to autogenerate classes based on an existing DB. For each of the tables I have documented them and each of their columns within SQL Server. Is there a way to use that documentation information to comment the generated classes and to populate the schema entity documentation? I see that there are meta tags that can be put in the hbm.xml mapping files, but since I have those autogenerated each time I'd need to either add them back in or continually merge in new changes, plus I'd ideally like to have the DB be the "truth" information and not store this sort of information in the mapping files. Does anyone know if this is possible and if so how to do it? Thanks...
Is there a way to use that documentation information to comment the generated classes and to populate the schema entity documentation?
To my knowledge, tables and columns comments are used in some of the the generated files, at least in the following templates from hibernate-tools.jar:
doc/tables/table.ftl
hbm/column.hbm.ftl
hbm/persistentclass.hbm.ftl
For example, in hbm/column.hbm.ftl:
<#if column.isFormula()>
<formula>${column.getFormula()}</formula>
<#else>
<column name="${column.quotedName}" ${c2h.columnAttributes(column)}<#if column.comment?exists && column.comment?trim?length!=0>>
<comment>${column.comment}</comment>
</column><#else>/>
</#if>
</#if>
But they aren't used in the templates for annotated POJOs, you'd have to modify the templates for this.
If you use Hibernate tool task to generate pojo classes out of HBM files and Database,It will add documentation in the generated java classes by default.You can see it in pojoTypeDeclaration ftl file.
/**
${pojo.getClassJavaDoc(pojo.getDeclarationName() + " generated by hbm2java", 0)}
*/
<#include "Ejb3TypeDeclaration.ftl"/>
${pojo.getClassModifiers()} ${pojo.getDeclarationType()} ${pojo.getDeclarationName()} ${pojo.getExtendsDeclaration()} ${pojo.getImplementsDeclaration()}
Where pojo.getClassJavaDoc will generate documentation if your hbm file has meta attribute declared such as CLASS_DESCRIPTION.

What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

I really want to know more about the update, export and the values that could be given to hibernate.hbm2ddl.auto
I need to know when to use the update and when not? And what is the alternative?
These are changes that could happen over DB:
new tables
new columns in old tables
columns deleted
data type of a column changed
a type of a column changed its attributes
tables dropped
values of a column changed
In each case what is the best solution?
From the community documentation:
hibernate.hbm2ddl.auto Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.
e.g. validate | update | create | create-drop
So the list of possible options are,
validate: validate the schema, makes no changes to the database.
create-only: database creation will be generated.
drop: database dropping will be generated.
update: update the schema.
create: creates the schema, destroying previous data.
create-drop: drop the schema when the SessionFactory is closed explicitly, typically when the application is stopped.
none: does nothing with the schema, makes no changes to the database
These options seem intended to be developers tools and not to facilitate any production level databases, you may want to have a look at the following question; Hibernate: hbm2ddl.auto=update in production?
There's also the value of none to disable it entirely.
The configuration property is called hibernate.hbm2ddl.auto
In our development environment we set hibernate.hbm2ddl.auto=create-drop to drop and create a clean database each time we deploy, so that our database is in a known state.
In theory, you can set hibernate.hbm2ddl.auto=update to update your database with changes to your model, but I would not trust that on a production database. An earlier version of the documentation said that this was experimental, at least; I do not know the current status.
Therefore, for our production database, do not set hibernate.hbm2ddl.auto - the default is to make no database changes. Instead, we manually create an SQL DDL update script that applies changes from one version to the next.
First, the possible values for the hbm2ddl configuration property are the following ones:
none - No action is performed. The schema will not be generated.
create-only - The database schema will be generated.
drop - The database schema will be dropped.
create - The database schema will be dropped and created afterward.
create-drop - The database schema will be dropped and created afterward. Upon closing the SessionFactory, the database schema will be dropped.
validate - The database schema will be validated using the entity mappings.
update - The database schema will be updated by comparing the existing database schema with the entity mappings.
The hibernate.hbm2ddl.auto="update" is convenient but less flexible if you plan on adding functions or executing some custom scripts.
So, The most flexible approach is to use Flyway.
However, even if you use Flyway, you can still generate the initial migration script using hbm2ddl.
I would use liquibase for updating your db. hibernate's schema update feature is really only o.k. for a developer while they are developing new features. In a production situation, the db upgrade needs to be handled more carefully.
Although it is quite an old post but as i did some research on the topic so thought of sharing it.
hibernate.hbm2ddl.auto
As per the documentation it can have four valid values:
create | update | validate | create-drop
Following is the explanation of the behaviour shown by these value:
create :- create the schema, the data previously present (if there) in the schema is lost
update:- update the schema with the given values.
validate:- validate the schema. It makes no change in the DB.
create-drop:- create the schema with destroying the data previously present(if there). It also drop the database schema when the SessionFactory is closed.
Following are the important points worth noting:
In case of update, if schema is not present in the DB then the schema is created.
In case of validate, if schema does not exists in DB, it is not created. Instead, it will throw an error:- Table not found:<table name>
In case of create-drop, schema is not dropped on closing the session. It drops only on closing the SessionFactory.
In case if i give any value to this property(say abc, instead of above four values discussed above) or it is just left blank. It shows following behaviour:
-If schema is not present in the DB:- It creates the schema
-If schema is present in the DB:- update the schema.
hibernate.hbm2ddl.auto automatically validates and exports DDL to the schema when the sessionFactory is created.
By default, it does not perform any creation or modification automatically on DB. If the user sets one of the below values then it is doing DDL schema changes automatically.
create - doing creating a schema
<entry key="hibernate.hbm2ddl.auto" value="create">
update - updating existing schema
<entry key="hibernate.hbm2ddl.auto" value="update">
validate - validate existing schema
<entry key="hibernate.hbm2ddl.auto" value="validate">
create-drop - create and drop the schema automatically when a session is starts and ends
<entry key="hibernate.hbm2ddl.auto" value="create-drop">
If you don't want to use Strings in your app and are looking for predefined constants have a look at org.hibernate.cfg.AvailableSettings class included in the Hibernate JAR, where you'll find a constant for all possible settings. In your case for example:
/**
* Auto export/update schema using hbm2ddl tool. Valid values are <tt>update</tt>,
* <tt>create</tt>, <tt>create-drop</tt> and <tt>validate</tt>.
*/
String HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
validate: validates the schema, no change happens to the database.
update: updates the schema with current execute query.
create: creates new schema every time, and destroys previous data.
create-drop: drops the schema when the application is stopped or SessionFactory is closed explicitly.
I Think you should have to concentrate on the
SchemaExport Class
this Class Makes Your Configuration Dynamic
So it allows you to choose whatever suites you best...
Checkout [SchemaExport]
validate: It validates the schema and makes no changes to the DB.
Assume you have added a new column in the mapping file and perform the insert operation, it will throw an Exception "missing the XYZ column" because the existing schema is different than the object you are going to insert. If you alter the table by adding that new column manually then perform the Insert operation then it will definitely insert all columns along with the new column to the Table.
Means it doesn't make any changes/alters the existing schema/table.
update: it alters the existing table in the database when you perform operation.
You can add or remove columns with this option of hbm2ddl.
But if you are going to add a new column that is 'NOT NULL' then it will ignore adding that particular column to the DB. Because the Table must be empty if you want to add a 'NOT NULL' column to the existing table.
Since 5.0, you can now find those values in a dedicated Enum: org.hibernate.boot.SchemaAutoTooling (enhanced with value NONE since 5.2).
Or even better, since 5.1, you can also use the org.hibernate.tool.schema.Action Enum which combines JPA 2 and "legacy" Hibernate DDL actions.
But, you cannot yet configure a DataSource programmatically with this. It would be nicer to use this combined with org.hibernate.cfg.AvailableSettings#HBM2DDL_AUTO but the current code expect a String value (excerpt taken from SessionFactoryBuilderImpl):
this.schemaAutoTooling = SchemaAutoTooling.interpret( (String) configurationSettings.get( AvailableSettings.HBM2DDL_AUTO ) );
… and internal enum values of both org.hibernate.boot.SchemaAutoToolingand org.hibernate.tool.schema.Action aren't exposed publicly.
Hereunder, a sample programmatic DataSource configuration (used in ones of my Spring Boot applications) which use a gambit thanks to .name().toLowerCase() but it only works with values without dash (not create-drop for instance):
#Bean(name = ENTITY_MANAGER_NAME)
public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory(
EntityManagerFactoryBuilder builder,
#Qualifier(DATA_SOURCE_NAME) DataSource internalDataSource) {
Map<String, Object> properties = new HashMap<>();
properties.put(AvailableSettings.HBM2DDL_AUTO, SchemaAutoTooling.CREATE.name().toLowerCase());
properties.put(AvailableSettings.DIALECT, H2Dialect.class.getName());
return builder
.dataSource(internalDataSource)
.packages(JpaModelsScanEntry.class, Jsr310JpaConverters.class)
.persistenceUnit(PERSISTENCE_UNIT_NAME)
.properties(properties)
.build();
}
To whomever searching for default value...
It is written in the source code at version 2.0.5 of spring-boot and 1.1.0 at JpaProperties:
/**
* DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto"
* property. Defaults to "create-drop" when using an embedded database and no
* schema manager was detected. Otherwise, defaults to "none".
*/
private String ddlAuto;
With all above said...
Notice this property is called dll.auto and should only control dll operations(create/drop schema/table), I found surprisingly that it has to do with dml, too: only update will allow insert data, which is dml operation.
Got caught by this when trying to populate data into a in-memory database; only update works.

Categories

Resources