Hibernate Named Query

I would like to share some basic of hibernate named query. It is very cool feature of java persistence or hibernate to query into the database with only creating the query once. Hare is my main class
package app.namedQuery;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NamedQuery;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class App {
	EntityManagerFactory emf;
	EntityManager em;

	public static void main(String[] args) {
		App temp = new App();
		temp.initEM();
		temp.doNamed();
	}

	public void initEM() {
		emf = Persistence.createEntityManagerFactory("ts");
		em = emf.createEntityManager();
	}

	public void doNamed(){
		Query mydo = em.createNamedQuery("MEMBER_ALL");
		List<Member> list =  mydo.getResultList();

		for(Member temp : list){
			System.out.println(temp.getEmail());
		}
	}
}

You can see below my member entity class, I use annotation rather than the mapping with the XML because it is not straight forward. Dont forget to add the below code into your entity class to enable the em.createNamedQuery to resolve the query

<code>@NamedQuery</code><code>(name=</code><code>"MEMBER_ALL"</code><code>, query=MyNamedQuery.SELECT_ALL_MEMBER)</code>
package app.namedQuery;

// Generated Sep 6, 2011 6:09:39 PM by Hibernate Tools 3.4.0.CR1

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.NamedQuery;

import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
 * Member generated by hbm2java
 */
@NamedQuery(name="MEMBER_ALL", query=MyNamedQuery.SELECT_ALL_MEMBER)
@Entity
@Table(name = "member", catalog = "test", uniqueConstraints = @UniqueConstraint(columnNames = "email"))
public class Member implements java.io.Serializable {

	private Long id;
	private String email;
	private String name;
	private String phoneNumber;

	public Member() {
	}

	public Member(String email, String name, String phoneNumber) {
		this.email = email;
		this.name = name;
		this.phoneNumber = phoneNumber;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "email", unique = true, nullable = false)
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "name", nullable = false, length = 25)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "phone_number", nullable = false, length = 12)
	public String getPhoneNumber() {
		return this.phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
}

I wrapped the query string into a class so that it is cleaner in your code, i put it inside a class and put the query string as static member so that can be accessed without getter.

package app.namedQuery;

public class MyNamedQuery {
	public static final String SELECT_ALL_MEMBER = "from Member";
}

I would like to give you the bonus of my persistence.xml conifugration for standalone application, please see below

<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistence deployment descriptor for dev profile -->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
	version="1.0">

	<persistence-unit name="ts"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<class>app.namedQuery.Member</class>
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
			<property name="hibernate.connection.username" value="teddybear" />
			<property name="hibernate.connection.password" value="******" />
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost/test" />
			<property name="hibernate.hbm2ddl.auto" value="update"/>
			<property name="hibernate.connection.autocommit" value="true"/>
			<property name="show_sql" value="true" />
		</properties>
	</persistence-unit>
</persistence>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s