Back Forum Reply New

JPA w/ Hibernate

Hi everybody,

I worked through the Spring 2 JPA example from IBM (developerworks/ed...spring2-i.html), and more or less built the same app.  I did however swap out Toplink and DB2 w/ Hibernate EM and MySQL 5.

Right now I just set it up to use 1 POJO.  Finds work perfectly (the findAll, findById and findByEmail), but the create and update will not persist -- they execute fine but give no results.

Here's the DAO class (implements a simple interface that I use to access it):Code:
public class UserDAO extends JpaDaoSupport implements UserService {

// create a new User
public User save(User user) {
getJpaTemplate().persist(user);
return user;
}

// delete an existing User
public void delete(User user) {
getJpaTemplate().remove(user);
}

// update a User
public User update(User user) {
return getJpaTemplate().merge(user);
}

// find all Users
public Listlt;Usergt; findAll() {
return getJpaTemplate().find(quot;select u from User uquot;);
}

// find a User based on the id
public User findById(Long id) {
return getJpaTemplate().find(User.class, id);
}

// find a User based on the email
public User findByEmail(String email) {
Listlt;Usergt; userList = getJpaTemplate().find(quot;select u from User u where u.email = ?1quot;, email);
if (userList != null amp;amp; userList.size() == 1) {
return (User)userList.get(0);
}
else {
return null;
}
}

}
Here's the POJO itself...Code:
@Entity
public class User {

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

@Column(length = 100)
private String name;

@Column(length = 100)
private String email;

@Column(length = 128)
private String password;

@Column
private Timestamp createDate;

@Column
private Timestamp lastLogin;

public User() {}

public User(String name, String email, String password,
Timestamp createDate, Timestamp lastLogin) {
this.name = name;
this.email = email;
this.password = password;
this.createDate = createDate;
this.lastLogin = lastLogin;
}

... getters/setters...
}
Here's the service bean config:Code:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans
.....gt;
  lt;bean id=quot;userServicequot; class=quot;com.musicstore.dao.UserDAOquot;gt;     lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt;  lt;/beangt;
  lt;bean id=quot;entityManagerFactoryquot; class=quot;org..orm.jpa.LocalContainerEntityManagerFactoryBeanquot;gt;     lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot;/gt;     lt;property name=quot;jpaVendorAdapterquot;gt;        lt;bean class=quot;org..orm.jpa.vendor.HibernateJpaVendorAdapterquot;gt;lt;property name=quot;showSqlquot; value=quot;truequot;/gt;lt;property name=quot;generateDdlquot; value=quot;truequot;/gt;lt;property name=quot;databasePlatformquot; value=quot;org.hibernate.dialect.MySQL5InnoDBDialectquot;/gt;        lt;/beangt;     lt;/propertygt;  lt;/beangt;

lt;bean id=quot;dataSourcequot; class=quot;org..jdbc.datasource.DriverManagerDataSourcequot;gt;
lt;property name=quot;driverClassNamequot; value=quot;com.mysql.jdbc.Driverquot; /gt;
lt;property name=quot;uclquot; value=quot;jdbc:mysql--localhost:3306/musicstorequot; /gt;
lt;property name=quot;usernamequot; value=quot;rootquot; /gt;
lt;property name=quot;passwordquot; value=quot;quot; /gt;
lt;/beangt;
  lt;bean id=quot;transactionManagerquot;     class=quot;org..orm.jpa.JpaTransactionManagerquot;gt;     lt;property name=quot;entityManagerFactoryquot; ref=quot;entityManagerFactoryquot;/gt;     lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot;/gt;  lt;/beangt;

lt;/beansgt;
Finally, here's what happens in the controller (in handleRequestInternal).  The find works but the update and create do not:Code:      Listlt;Usergt; users = userService.findAll();
   for (User user : users) {   user.setLastlogin(new Timestamp(System.currentTimeMillis()));   user = userService.update(user);   }      User user = new User(quot;bobquot;, quot;bobquot;, quot;bobquot;, new Timestamp(System.currentTimeMillis()), new Timestamp(System.currentTimeMillis()));   user = userService.save(user);
Thanks!

If I need to post any more information about this, let me know.

Thanks

It is all about transactions and I expect that  your transactions are not properly configured. There is only a TransactionManager configured but there is no transactional configuration. No transaction - no data modification....

Next to that you need to tell your persistence provider to use LOCAL transactions (that you configure in you orm.xml).

When create executes fine but the object(s) are not created in the database it means the transaction is not being committed.

The link you supplied requires an IBM password and I don't have one so could not look at the example. Check your transaction handling...

Andy.

Thanks guys, I guess they just left the transactional information out of the paper (left as an exercise for the reader, LOL).  All I had to do was define the tx namespace and put a lt;tx:annotation-driven/gt; into my bean config xml, and add the @Transactional annotation to the service implementation.

Dang, this stuff is sweet.  Especially when compared to EJB 1.x...
¥
Back Forum Reply New