JPA Configuration in Spring 2.5

Source: Internet
Author: User
Tags glassfish in domain

This article provides a simple Spring framework standalone environment, how to step-by-step to develop JPA wizards. The JPA specification was first created by the persistence mechanism of EJB 3.0, which is recognized as a mechanism for the simple pojos persistence. You can start to feel the power of JPA in your favorite IDE by configuring a little Spring bean with a few jars in the classpath. Here we are using Glassfish JPA-an open source project based on the Oracle ' s TopLink ORM framework.

Initialization settings

Make sure you are using Java 5 (a prerequisite for JPA in EJB 3.0).

Download GlassFish JPA jar from https://glassfish.dev.java.net/downloads/persistence/JavaPersistence.html (note: I'm using "v2_build_ 02″jar, but this version should also be back-compatible.)

Extract from the "installer" Jar and run: Java-jar Glassfish-persistence-installer-v2-b02.jar

Add Toplink-essentials.jar to your classpath.

Add the database driver JAR (I'm using version 1.8.0.1 's Hsqldb.jar as an example, but in fact you can fit into another database with little change)

Join 2.0 M5 above version of Spring JAR (http://sourceforge.net/project/showfiles.php? group_id=73357)-Spring.jar-spring-jpa.jar-s Pring-mock.jar

Finally, add these jars to your classpath:-Commons-logging.jar-log4j.jar-junit.jar

Domain models (domain model)

In this example we only have a destination listing 3 simple domain model. Note that we use annotation in this example. When using JPA, you typically choose to use annotation or XML files, or both, to specify ORM (object-relational mapping) meta data. Here, we just chose to use the annotation alone, because we just need to add a short description to the code in domain model to be able to do it right away. First, look at the restaurant restaurant class:

package blog.jpa.domain;
import java.util.Set;
import  javax.persistence.CascadeType;
import javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import  javax.persistence.Id;
import javax.persistence.JoinColumn;
import  javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import  javax.persistence.OneToOne;

@Entity
public class Restaurant  {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long  id;

private String name;

@OneToOne(cascade = CascadeType.ALL)
private  Address address;

@ManyToMany
@JoinTable(inverseJoinColumns = @JoinColumn(name  = "ENTREE_ID"))
private Set entrees;

public long getId() {
return  id;
}

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

public  String getName() {
return name;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.