You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Apache Wiki <wi...@apache.org> on 2007/12/11 10:00:13 UTC
[Tapestry Wiki] Update of "Tapestry5HowToIocAndHibernate" by DavorHrg
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.
The following page has been changed by DavorHrg:
http://wiki.apache.org/tapestry/Tapestry5HowToIocAndHibernate
New page:
This article continues from [Tapestry5HowToIocOnly] and adds tapestry-hibernate to the app.[[BR]]
Tapestry hibernate is currently dependant on tapestry-core (which will hopefully be fixed later).[[BR]]
We will add a dependancy to tapestry-core, but will not load whole !TapestryModule.
Only some parts of the code will be provided in the page, and the rest as attachment.
For tapestry-hibernate to work without tapestry-core
you need some extra setup in !AppModule.
definitely make sure to call {{{registry.cleanupThread();}}} at the end of the main method
or your data will not be saved (tapestry-hibernate performs session.commit() when that event occurs)
{{{#!java
public class MiniAppModule {
public static void bind(ServiceBinder binder){
binder.bind(Hello.class);
//tapestry-hibernate needs this dependency
binder.bind(ClassNameLocator.class, ClassNameLocatorImpl.class);
}
public static void contributeSymbolSource(OrderedConfiguration<SymbolProvider> conf){
//tapestry-hibernate fails without tapestry.app-name symbol defined
conf.add("AppPackage", new SymbolProvider(){
public String valueForSymbol(String symbolName){
if(symbolName.equalsIgnoreCase(InternalConstants.TAPESTRY_APP_PACKAGE_PARAM))
return "tapestry.mini";
return null;
}
},"");
}
public void contributeHibernateEntityPackageManager(Configuration<String> configuration)
{
// extra packages...
// tapestry-hibernate will add entities package automaticaly for TAPESTRY_APP_PACKAGE_PARAM+".entities"
// configuration.add("tapestry.mini.entities");
}
}
}}}
Hello class is bit different for this demo
{{{#!java
public class Hello {
private final UserDao _userDao;
public Hello(UserDao userDao){
_userDao = userDao;
}
public void sayHello(){
List<User> list = _userDao.findAll();
if(list.size() == 0)
System.out.print("No users in database");
else
System.out.print("Hello "+list.get(0).getName());
}
}
}}}
User entity is just something simple
{{{#!java
@Entity
public class User {
Long id;
String name;
String username;
public User(){}
public User(String name, String username) {
super();
this.name = name;
this.username = username;
}
@Id @GeneratedValue
public Long getId() {
return id;
}
//... other getters/setters
}
}}}
User dao just extends [http://www.hibernate.org/328.html Generic Data Access Objects] from [http://www.hibernate.org]
{{{#!java
public class UserDAO extends GenericHibernateDAO<User, Long> {
//if you forget this constructor Session will not be injected
public UserDAO(Session session) {super(session);}
//example for search by criteria
public List<User> findByName(String name){
//findByCriteria is hibernate specific so you can not call _userDao.findByCriteria from
//outside this class, and you should not expose it either
return this.findByCriteria(Restrictions.eq("name", name));
}
}
}}}
run the app twice, first time it will create an user in the database, second time it will say Hello John.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org