Archive for the ‘Spring’ Category

How to issue a JSON POST request to Spring Data Rest with a JPA relation

25 November 2014 Leave a comment

Lets say we have a class Company which has a relation to a class User

@Table(name = "company")
public class Company implements {

	private static final long serialVersionUID = 1L;
	private Long id;
	private String name;
	private User user;

	public Company() {

	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {

	public void setId(Long id) { = id;

	@Column(name = "name", nullable = false, length = 200)
	public String getName() {

	public void setName(String name) { = name;

	@ManyToOne(fetch = FetchType.LAZY, optional = false, targetEntity=my.example.User.class)
	@JoinColumn(name = "user_id")
	public User getUser() {
		return user;

	public void setUser(User user) {
		this.user = user;

and their corresponding repositories:

@RepositoryRestResource(collectionResourceRel = "company", path = "company")
public interface CompanyRepository extends JpaRepository<Company, Long>  {

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepository extends JpaRepository<User, Long> {


In order to add a user to the company (let’s say we want to link this company to the user with id of 2) you will need to issue the following JSON request to the company repository:

"name" : "a company name",
"user" : "http://localhost:8086/user/2"

The “user” variable should be the URL that is pointing to the user repository.

Categories: Java, Spring, Spring Data Rest Tags: ,

JEE presentation

1 December 2008 1 comment

Today I was invited to the City university to give a small talk about enterprise Java. It was my first ever talk in a university (it was quite different from giving presentation at work), and I was a bit nervous. Hope I did well because I really enjoyed it and I met interesting people. Thanks to everyone for coming.

If you would like to download the talk please follow the link: JEE

Categories: Java, Spring Tags: , ,

How to create a TopLink EntityManagerFactory with Spring outside a J2EE container

6 September 2008 Leave a comment

Sometimes you need to create a JPA EntityManagerFactory outside a J2EE container. One of the cases is if you are using Spring and jpa with a standalone application. As the jdocs for the JpaTemplate state the preferred way is to use a LocalContainerEntityManagerFactoryBean and set the required properties. The example code below demonstrates the creation of an EntityManagerFactory by using HSQLDB

DataSource datasource = new DriverManagerDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:test", "user", "password");
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setJpaVendorAdapter(new TopLinkJpaVendorAdapter());
EntityManagerFactory factory = bean.getObject();

First we create a DriverManagerDataSource (from the Spring framework) and supply the required arguments (driver class, database url, username and password) and then we create an instance of the LocalContainerEntityManagerFactoryBean. We need to set the jpa vendor adapter (we can use Spring’s TopLinkJpaVendorAdapter which is Spring’s implementation for Oracle TopLink Essentials) and the datasource. Then we call the afterPropertiesSet() method which means that the bean instance will only perform initialisation after all properties are set (in the event of misconfiguration it will throw an exception).

This is it. Once we have the EntityManagerFactory we can use it to create a JpaTemplate

JpaTemplate jpa = new JpaTemplate(factory);

or if you programme to interface (which you should) the preferred way of doing this is

JpaOperations jpa = new JpaTemplate(factory);

Also it’s always proper to close the factory after you are done processing since leaving it open can result in a PersistenceException exception with error message “Exception Description: Attempted to deploy PersistenceUnit <unit name> while being in the wrong state [Undeployed]. Close all factories for this PersistenceUnit. “

If you get this error you need to close the factory after you are done processing

Categories: Hsqldb, Java, Spring, TopLink

SpringSource seminar – Athens – Greece

19 July 2008 3 comments

The Hellenic Java User Group and SpringSource are proud to present the free Spring Seminar. This Seminar will be a 4-hour seminar with 2 talks of Iwein Fuld, consultant at SpringSource based around the Spring Portfolio and the SpringSource Application Platform. The sessions will start around 1.30 PM and ends at 6 PM.

In addition to the SpringSource Seminar, SpringSource organizes a 4-day Core Spring training in Athens, during 26-29 of August. For more information: Take advantage of the Early Bird discount, register before July 26th and save EUR 300!

During the SpringSource Seminar, the following sessions will be presented;

Spring Portfolio; Current state and beyond

The Spring Framework 2.5 introduced comprehensive support for annotation-based configuration, along with full support for the Java 6 and Java EE 5 platforms. Now Spring is preparing for the Spring 3.0 release, introducing further annotation-based configuration options and unified expression language support. This talk discusses Spring as a modern Java 5 oriented application framework – covering the core component model, integration with common technologies such as JPA and JSF, as well as Spring’s annotation-driven web MVC.

Application Platform

If you’re a Java web developer, you’re certainly familiar with monolithic WAR deployments and library bloat, and you’ve probably thought numerous times, “There must be a better way.” Well, there is! By building on the benefits of an OSGi runtime environment and combining the Spring and Spring-DM programming models, the SpringSource Application Platform offers enterprise web developers exciting new opportunities. This session will focus on developing web applications in an OSGi environment and will include a discussion of the migration path from a standard Java EE WAR to a fully OSGi-enabled web application packaged as a Web Module within a PAR. We will begin with an overview of deployment and packaging options available on the Platform and then take a closer look at each supported web deployment model from Standard WARs to Shared Libraries WARs, Shared Services WARs, and finally Web Modules. Attendees will walk away with a solid understanding of how to both develop and deploy next generation web applications on the S2AP.

More information can be found here.

Categories: Java, JHUG, Spring