Using #Entity Class When Table Doesn't Exist - java

I have several database tables that my Spring MVC/JPA application refers to using the #Entity and #Table Annotations. I've run into the issue where if my application switches between database connections, some tables that exist on database 1 may not exist in database 2 (as we are following the SDLC cycle and promoting table additions/changes after they get the "OK"), thus resulting in an SQL Exception when the application server starts.
Does spring offer a way to mark specific #Entity Classes as "Optional" or "Transactional" so there are no database Exceptions returned because of nonexistant tables?

In my opinion, there is no option to do that.
You can add automatic update of schema in Hibernate, but you mentioned that you are doing this manually.
Hibernate is validating the schema, when he establishes connection. You use #Entity, so he looks for that table and throws an error if there is no with the name specified.

Related

HSQLDB: case sensitive table names

I have the following jpa/hibernate/hsqldb configuration:
JPA ddl-auto: create-drop
Hibernate entities have no #Table annotation and created with SpringPhysicalNamingStrategy. So, PersonalData entity table name is personal_data. Hibernate creates them due to running the application
hsql DB URL is jdbc:hsqldb:mem:testdb;sql.syntax_pgs=true
My problem is when I try to select due spring repositories with the hsql there is the error about non-existing PERSONAL_DATA table.
I found that this is SQL notation to use CASE_SENSETIVE tables and hqsl follows that. To resolve that developers offer quote table names in sql.
So, I have 2 unlikely ideas
Add #Table annotation to entities.
Override SpringPhysicalNamingStrategy
Is there a way to use a simple property?

Custom Validation Using Hibernate Validator

We have an Application that uses a NoSQL DB as its data source for whatever reason.
We need to persist say E.g an Employee Object that has a Department id and an Address with a zip code. If I persist it in an RDBMS with an invalid department id and zipcode. the DB will throw a constraint exception.
But if I use a NOSQL DB I cannot do this Validation. Is there is any way I can use Hibernate validator and check in DB whether a zip code or department id exist in the DB. Before saving it in DB
Here's an example on the JBoss developer forums that describes how to access the Hibernate Session in a custom validator. It's a bit dated, so you may have to make some tweaks.

How to Solve SQL Integrity Constraint Violation Exception

I created an entity class that has the same properties as project.java, and created a class where I can persist the entity object. Also, I created a database using databases in Netbeans using embedded JDBC. I have the persistence.xml, which provides the properties to connect the db, and is used the persitence class on entitymanagerfactory object. The connection seems fine but I am having Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'PROJECT_ID' cannot accept a NULL value. error.
Is it ok to create the db manually (executing the ddl) or should I create the table in the persistence.xml using property name="javax.persistence.jdbc.url"" value="create-tables ?
Regards
You have to set the value of the column PROJECT_ID.
You can either do this in your code, for example by using the annotations #SequenceGenerator or #GenericGenerator,
or use db features (trigger) to set the id during insert.

Hibernate reveng persistence model does not validate

I use Hibernate Reverse Engineering to automatically create classes from a database scheme. DB server is MSSQL 2008. This database is designed by a partner and could potentially change without notice. Thus I'd like to have Hibernate validate the scheme on startup, wich in my opinion should work out of the box. But it doesn't:
org.hibernate.HibernateException: Wrong column type in somedb.dbo.ASVC_S for column SomeCol. Found: decimal, expected: numeric(18,0)
The generated enttity class looks like this:
#Column(name="SomeCol", precision=18)
public BigDecimal getSomeCol() {
return this.someCol;
}
Is my assumption that reveng creates classes that can be validated against the schema wrong? Should I skip validation and hope that during runtime everything's OK? Annotating the classes after generating them or maintaining an entry for each class in my reveng.xml mapping file is not an option - too many classes ;)
hibernate-tools is version 4.0.0-CR1.

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

Categories

Resources