Friday, May 16, 2014

Hibernate relationship templates

Here is a list with one-to-many, one-to-one and many-to-many relations templates.

One-to-many:
@Entity(name = "parent")
@Access(AccessType.FIELD)
public class Parent {

    @OneToMany(fetch = FetchType.LAZY, mappedBy="childVarName", orphanRemoval=true)
    protected Set<Child> parentVarName;
}

@Entity(name = "child")
@Access(AccessType.FIELD)
public class Child {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "child", nullable = false)
    protected Parent childVarName;
}

One-to-one:
public class VirtualParent {
    @OneToOne(mappedBy = "virtualParentVar")
    protected VirtualChild virtualChildVar;
    // ...
}

public class VirtualChild {
    @OneToOne
    @JoinColumn(name = "frn_virtual_parent_id")
    protected VirtualParent virtualParentVar;
    // ...
}

Many-to-many:

Wednesday, May 14, 2014

Setting transactional Spring components manually

There is an issue while setting @Transactional Spring components, because of proxies used to generate them. Here is an solution for that:

Setter:        
ReflectionTestUtils.setField( 
    TestUtils.unwrapTransactionalServiceSafe(serviceWithAutowiredComponents), 
    "externalService", newExternalServiceInstance );


Utils:
    public static Object unwrapTransactionalService( Object transactionalService ) throws Exception {
      if(AopUtils.isAopProxy(transactionalService) && transactionalService instanceof Advised) {
          Object target = ((Advised) transactionalService).getTargetSource().getTarget();
          return target;
      }
      return null;
    }
    public static Object unwrapTransactionalServiceSafe( Object transactionalService ){
        try {
            return unwrapTransactionalService( transactionalService );
        } catch (Exception ex) {
            throw new RuntimeException( ex );
        }
    }



Tuesday, May 13, 2014

Spring: setting properties externally

        ReflectionTestUtils.setField( mobileController, "externalService", externalService );

Tuesday, April 22, 2014

Ways of Inheritance in Hibernate

Here is a great article for hibernate inheritance.

An example  of each of them is added. Advantages and disadvantages also are mentioned.

Using Subversion in Netbeans - solutions for all issues

Some mandatory steps needed after Netbeans installation to get SVN working.

0. Start position: svn functionality is not working. Item "Show annotations" in Team menu is deactivated.

1. Your subversion client version must match requirements by Netbeans. They are different for every Netbeans version. So check link - http://wiki.netbeans.org/FaqSubversionClients

2. Show annotations menu item in Team menu is active now.

3. Navigate menus: Tools > Options > Team -> Versioning > Subversion . Select "Path to the SVN executable file". It is placed in folder C:\Program Files\TortoiseSVN\bin by default. If executable could not be selected, may not be installed in SVN installation. Ensure "command line utils" checkbox is selected in the reinstallation.

4. Update or any SVN action could cause "sslprotocolexception: handshake alert:  unrecognized_name" in Netbeans. This is caused by missing configuration in Netbeans.
Solution:
Copy folders named: svn.ssl.client-passphrase, svn.ssl.server, svn.username
from folder: C:\Documents and Settings\<username>\Application Data\Subversion\auth
to folder: C:\Documents and Settings\<username>\AppData\Roaming\NetBeans\8.0\config\svn\config\auth

5. Finally solved! So enjoy your SVN client.

Spring repositories with native queries - Supported keywords inside findBy method names



KeywordSampleJPQL snippet
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
BetweenfindByStartDateBetween… where x.startDate between 1? and ?2
LessThanfindByAgeLessThan… where x.age < ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
After
findByStartDateAfter… where x.startDate > ?1
Before
findByStartDateBefore… where x.startDate < ?1
IsNullfindByAgeIsNull… where x.age is null
IsNotNull,NotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1 (parameter bound with appended %)
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1 (parameter bound with prepended %)
ContainingfindByFirstnameContaining… where x.firstname like ?1 (parameter bound wrapped in %)
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection<Age> ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection<Age> age)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false


Source: http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html

Wednesday, April 16, 2014

java syntax for catching multiple exceptions

New java syntax for catching multiple exceptions in a single section:

    try {
        // do something
    } catch (ImageProcessingException | IOException ex) {
        log.error("Unable to get orientation from file " + imagePath.toString(), ex);
    }