<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: How to access database in a faces Validator</title><link>https://answers.axonivy.com/questions/3364/how-to-access-database-in-a-faces-validator</link><description>&lt;p&gt;Hi all, I am new to AXON IVY. Hope you can be patient with me.&lt;/p&gt;
&lt;p&gt;I am trying to use that code here: &lt;a href="https://answers.axonivy.com/questions/314/using-db-connection-from-pis"&gt;https://answers.axonivy.com/questions/314/using-db-connection-from-pis&lt;/a&gt; (provided by Ivy Support Team) in a Java class in a CustomValidator class to look up a database table. 
Somehow I could not resolve the Ivy object. &lt;/p&gt;
&lt;p&gt;I tried looking up getProcessEngineHook method in &lt;a href="https://developer.axonivy.com/doc/latest/PublicAPI"&gt;https://developer.axonivy.com/doc/latest/PublicAPI&lt;/a&gt; but the search doesn't return any results.&lt;/p&gt;
&lt;p&gt;My codes as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;package trial.facesValidation;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import  java.sql.Connection;

// To validate if Item Name already existed in Parts table in Database
@FacesValidator("trial.facesValidation.CustomItemValidator")
public class CustomItemValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException 
    {
        //connect to the database
        Connection dbconnection = ivy.getProcessEngineHook().getDbConnection("TrialDBConn");

        try
        {
        // execute some sql queries
        } finally {
        // close the connection
              dbconnection.close();
        }
&lt;/code&gt;&lt;/pre&gt;</description><atom:link href="https://answers.axonivy.com/questions/3364/how-to-access-database-in-a-faces-validator" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 24 Jul 2018 09:14:35 -0400</lastBuildDate><item><title>Answer by Reguel Wermelinger</title><link>https://answers.axonivy.com/questions/3364/how-to-access-database-in-a-faces-validator/3365</link><description>&lt;p&gt;I think the code above is outdated since Axon.ivy 6.0. &lt;/p&gt;
&lt;p&gt;But it should be possible to solve this problem by using JPA: &lt;a href="https://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.datamodeling.html#ivy-persistence-api"&gt;https://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.datamodeling.html#ivy-persistence-api&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Then you could easily access the persistence unit or the EntityManager itself and query any objects out of the database. Similar as shown here: &lt;a href="https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager/1103"&gt;https://answers.axonivy.com/questions/1102/how-to-use-jpa-entity-manager-instead-of-ivy-entity-manager/1103&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The rough steps to use JPA in an ivy project:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;define an external database as data
    store:
    &lt;a href="https://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.configuration.html#ivy-database"&gt;https://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.configuration.html#ivy-database&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;map it to a persistence unit provider:
    &lt;a href="https://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.datamodeling.html#ivy-editors-persistence-configuration"&gt;https://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.datamodeling.html#ivy-editors-persistence-configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;generate JPA pojos: &lt;a href="https://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.datamodeling.html#ivy-persistence-entityclasses"&gt;https://developer.axonivy.com/doc/latest/DesignerGuideHtml/ivy.datamodeling.html#ivy-persistence-entityclasses&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Tue, 24 Jul 2018 09:14:35 -0400</pubDate><guid>https://answers.axonivy.com/questions/3364/how-to-access-database-in-a-faces-validator/3365</guid></item></channel></rss>