Standalone Maven + Spring + Spring ORM + Hibernate Example!

Sakila DB, of course, but.. but.. where is my persistence.xml?

Directory Layout

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
 
    <groupId>biz.tugay</groupId>
    <artifactId>sprngormexample</artifactId>
    <version>1.0-SNAPSHOT</version>
 
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.5.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>mvn</executable>
                    <mainClass>biz.tugay.sprngormexample.MainClass</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

hibernate.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sakila
jdbc.username=root
jdbc.password=root
 
# Hibernate Properties
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=false

beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:property-placeholder location="classpath:/META-INF/hibernate.properties"/>
 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
 
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="annotatedClasses" value="biz.tugay.sprngormexample.Actor"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
    </bean>
</beans>

Actor.java
package biz.tugay.sprngormexample;
 
import javax.persistence.*;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 11/13/2016
 * Time: 16:33 PM
 */
@Entity
public class Actor {
 
    private Integer id;
    private String firstname;
    private String lastname;
 
    @Id
    @Column(name = "actor_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer actor_id) {
        this.id = actor_id;
    }
 
    @Basic
    @Column(name = "first_name")
    public String getFirstname() {
        return firstname;
    }
 
    public void setFirstname(String first_name) {
        this.firstname = first_name;
    }
 
    @Basic
    @Column(name = "last_name")
    public String getLastname() {
        return lastname;
    }
 
    public void setLastname(String last_name) {
        this.lastname = last_name;
    }
}

MainClass.java
package biz.tugay.sprngormexample;
 
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import java.util.List;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 11/13/2016
 * Time: 16:29 PM
 */
public class MainClass {
 
    public static void main(String[] args) {
        final ClassPathXmlApplicationContext classPathXmlApplicationContext
                = new ClassPathXmlApplicationContext("/beans.xml");
        final SessionFactory sessionFactory
                = (SessionFactory) classPathXmlApplicationContext.getBean("sessionFactory");
 
        final Session session = sessionFactory.openSession();
 
        final Query query = session.createQuery("SELECT a FROM Actor a");
        final List<Actor> list = query.list();
        final Actor firstActor = list.iterator().next();
        System.out.println(firstActor.getFirstname() + " " + firstActor.getLastname());
        session.close();
        sessionFactory.close();
        System.exit(-1);
    }
}

In Action