Custom Validation Using Hibernate Validator - java

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.

Related

How use Hibernate Envers with Teiid

I have a problem with the integration of hibernate envers on a application that use Teiid. This one doesn't support ddl like 'create table TABLE_NAME_AUD' or sequence generation.
I tried to split entity manager and tranction manager and delegate create table to another entity manager, but the transations on entities created by virtual table doesn't work. I also tried to create tables on DB, but the insert statement generated by hibernate dosn't work because the name of column of reventity is a keyword of teiid syntax and I can't change the name of column.
So, there's a way to implements hibernate envers with Teiid propertly?
Thanks!

Use two entity managers for one entity

The situation is the following: I have two databases Db1 and Db2, for which I have two EntityManagers em1 and em2 defined. Furthermore, I have the entity Person(int id, String name, Pet pet) mapped to the table persons(id, name) in the database Db1 and the entity Pet(int id, String name, Person owner) mapped to the table pets(id, name, person_id) in the database db2. The relation between Person and Pet is a #OneToOne realtion. At some point in the program, I would like to do something like this:
PersonDAO personDAO = db1DAOFactory.getPersonDAO();
Person person = personDAO.find(100);
System.out.println(person.getPet().getName());
There is no possibility to merge both databases. How can I tell Hibernate to use for the pet field the EntityManager em2? With Hibernate I use only annotations, no xml configuration.
Thank you very much!
Sorry but I think you can't. The question is answered by nakosspy:
Hibernate will create an SQL query for your HQL or Criteria query and this SQL will be sent through jdbc to the database. This means that hibernate does not support for what you are trying to do.
However, you can achieve the same result in some cases. Some databases give you the option to create an alias for a table that resides in a different database. So you will be able to write an SQL query that joins the two tables and execute it on the database.
We are doing that with DB2. If you can do that, it depends on your database.
I guess, that it would impossible if you have two different databases (example DB2 and MySQL) but if both databases are of the same vendor, then maybe it's achievable.
You should try to find more info in you database server's documentation.
If you want to see his original post check this link.
This is dependent on database features. If you are using Oracle database, You could achieve this by creating db link in database then map these entity through creating a view or a synonym in oracle database. Check your database features. Hope this helps.

Using #Entity Class When Table Doesn't Exist

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.

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.

Persisting with Entity Manager into MySQL on a table with auto increment PK

I am following the NetBeans E-commerce tutorial - on the 9th Tutorial which is about Integrating Transnational Business Logic
Where they show how create the OrderManager class with placeOrder() Method - and the method is transactional which involves three tables - first customer, then customer_order and finally orderedItem using em.persist().
but the em.persist() method is not persisting for customer - but it will persist for customer if I manually supplied the customer id manually into the code (hard code).
but for the customer_order it will not persist even after persisting the customer by manual id provision and using em.flush();
I googled and couldn't seem to find way out. P.S. The Entity class is generated with Netbeans wizard - and the id generation strategy IDENTITY
The em.persist() was not persisting because the #NotNull annotation on the id fields was not allowing null - as I am working on Netbeans.
so removing those #NotNull or commenting them out on the entity class gets the job done.
infact I learned this fact from the last post of the following link.
Hibernate Auto Increment ID

Categories

Resources