An alternative to Hibernate native Query

Hi,

In one of the projects I am working on I have the following scenario:

A Domain entity with many to many relationship to a  SyslogServer entity:

//domain
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.MERGE})
@JoinTable(name = "NEO_DOM_SYSLG_MAP",
 joinColumns = {@JoinColumn(name = "DOMAIN_ID", unique = false)},
 inverseJoinColumns = {@JoinColumn(name = "SYSLOG_ID", unique = false)})
 public Set<SyslogServer> getSyslogServers() {
 return _syslogServers;
 }
 

//syslog

@ManyToMany(mappedBy = "syslogServers")
 public Set<Domain> getDomains() {
 return _domains;
 }

 

Now given an ID of a syslog server, I need to find if at all it is being used by any Domain entity. The purpose is to enable/disable a delete button on a JSF page whenever the entity is not in use. 

The solution I came up with uses a native query and  is as follows:

@SuppressWarnings("unchecked")
    /*
     */
    public List<SyslogServer> findBySyslogId(SyslogServer sysLog) {
        _log.debug("Executing findBySyslogId for syslog:" + sysLog);

        Query q = _entityManager.createNativeQuery(
                "SELECT o.* FROM NEO_SYSLOG o, NEO_DOM_SYSLG_MAP i WHERE (o.ID=i.SYSLOG_ID) AND o.ID=:SYSLOG_SERVER_ID_PARAM",
                SyslogServer.class);

        q.setParameter("SYSLOG_SERVER_ID_PARAM", sysLog.getId());

        List sysLogServers = q.getResultList();

        if (sysLogServers != null) {
            _log.debug("Collection size:" + sysLogServers.size());
            return ((List<SyslogServer>) sysLogServers);
        }
        //Avoid NPE
        _log.debug("Returning empty collection.");
        return new ArrayList<SyslogServer>(0);
 } 

I am trying to find an alternative solution which would be as fast but would not involve a native query.  Any ideas are welcomed, just make sure you understand the scenario.

Shlomo.

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com