myeclipse下整合spring和hibernate(编辑修改稿)内容摘要:

,程序会向表 Content插入 100 条记录。 祝你好运。 ,现在也贴出来,希 望对大家有所帮助: 如果运行时出现如下异常 Could not find UserTransaction in JNDI :… 解决: 把配置文件“ ”中的 property name= /property property name= java:p/UserTransaction /property 两行设置 JTA控制事务的属性注释掉就可以了,因为程序里还未使用 JTA控制事务。 如果运行时出现如下异常 Error parsing XML: /(33) The content of element type sessionfactory is inplete, it must match (property*,mapping+,(classcache|collectioncache|jcsclasscache|jcscollectioncache)*). 解决: 在 配置文件“ ”中的 sessionfactory与 /sessionfactory之间加 mapping resource= / 第一次写关于 Hibernate 的文章,如有不对的地方,请批评指正。 最后,祝大家成功。 ^_^ hibernatesynch On this page, I will walk through a few features of the plugin and how it is intented to work. Create a Configuration File  Create a Java project and add the jar containing the driver in the project39。 s build path.  Start the wizard by clicking File New Other Hibernate Configuration File.  Select the default package for the file container and let the wizard do the rest. Create Mapping Files  Start the wizard by clicking File New Other Hibernate Mapping File.  Set the database information and click Refresh.  Select any tables you would like to create mapping files for.  Set the package you would like the business objects to reside in.  Select the mapping files in the navigator, right click and select Hibernate Add Mapping Reference. Use the Generated Objects Assuming a simple example like person, the following snippet is an example of how the generated code can be used: // load the configuration file ()。 PersonDAO dao = new PersonDAO()。 // find all people List people = ()。 // find a single person Long id = new Long(3)。 Person person = (id)。 // update the person (Joe)。 (person)。 // delete the person (id)。 Spring+Hibernate+Junit+Oracle 的最简单的人门 一年前,就计划学习 Spring,但初步 了解一下,发现还是有些庞大,就知难而退了。 最近反思一年来开发进展迟缓的原因,发现技术学习上比较保守,是重要原因。 学习主流的开发技术,虽然有学习成本,但会有开发效率的提升。 第一个目标:用 Spring+ Hibernate 完成对持久层的改造,这是 Spring 最迷人的应用之一吧。 Spring 比较复杂,有七大部分,比如 ORM/MVC/WEB 等等,我想,应该分步学习,有些部分可能我以后也不打算学习,比如其 MVC 部分。 首先,作一点理论准备。 SpringFramework 的官方站点查阅资料,初步了解一下 IOC,AOP这些 SpringFramework 力推的核心概念。 阅读了官方手册,以及《 Spring In Action》的部分内容,以及 Martin Fowler 谈 IOC(Dependency Injection)的文章。 其次,寻找可用的入门教程,最后锁定了 IBM developworks 上的一篇文章,以及 SpringFramework 自带的 petclinic 作为实战材料。 Objectrelation mapping without the container- Develop a transactional persistence layer using Hibernate and Spring 这篇文章非常符合我的目标。 透明思考称该文的作法是: “ Spring 结合 Hibernate 的标准做 法”。 我的开发环境是: Jbuilder X+Oracle 8i +Jboss ,操作系统是 TurboLinux Server。 下载 Richard HighTower 的源代码。 让这个例子运行起来,居然费了我一整天的时间。 HighTower 的例子,其数据库用的是 Mysql,而我的是 Oracle 8i,单单这个差别,就浪费了我一天的时间,因为 Dbunit 操作 Mysql 与 Oracle 有些细节地方不同,就会导致运行失败。 为了让和我一样的初学者少走点弯路,下面给出我改造过的例子,尽可能的简 化再简化,因此本例特地只使用一个数据库表: hofman_user。 1. 建立数据表 create table hofman_user (pk_user_id number(8), vc_ varchar2(50), vc_password varchar2(80), primary key(pk_user_id))。 2. 建立 hofman_user 表主键 pk_user_id 使用的序列 create sequence hofmanid_seq。 3. 一个数据表对应一个 POJO: package hofman。 import。 import。 public class User { private Long id = new Long(1)。 private String。 private String password。 public Long getId() { return id。 } public void setId(Long long1) { id = long1。 } public String getEmail() { return。 } public String getPassword() { return password。 } public void setEmail(String string) { = string。 } public void setPassword(String string) { password = string。 } } 4. 对应的 hibernate 的映射文件: ?xml version=? !DOCTYPE hibernatemapping PUBLIC //Hibernate/Hibernate Mapping DTD hibernatemapping class name= table=HOFMAN_USER dynamicupdate=false dynamicinsert=false discriminatorvalue=2 id name=id column=PK_USER_ID type= unsavedvalue=1 generator class=sequence param name=sequencehofmanid_seq/param /generator /id property name= type=string update=false insert=true column=VC_EMAIL length=82 notnull=true unique=true / property name=password type=string update=false insert=true column=VC_PASSWORD length=20 notnull=true unique=true / /class query name=AllUsers![CDATA[ from User user order by asc ]]/query /hibernatemapping 5. UserDAO 的 interface 文件: package hofman。 import。 public interface UserDAO { public List getUsers()。 public void addUser(User user)。 public User findUserByEmail(String )。 public void removeUser(String )。 } 6. UserDAO 的实现文件: package hofman。 import。 import。 import。 import。 import。 import。 import。 import。 public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{ public List getUsers(){ return getHibernateTemplate().findByNamedQuery(AllUsers)。 } public User findUserByEmail(String ) { return (User) getHibernateTemplate().find(from User u where =?,).get(0)。 } public void addUser(User user){ getHibernateTemplate().save(user)。 } public void removeUser (String ) { User user = findUserByEmail()。 ().delete(user)。 } } 7. SpringFramework 的配置文件: . ?xml version= encoding=UTF8? !DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN ! Application context definition for Express on Hibernate. beans ! bean id=dataSource class= property name=jndiName valuejava:/p/env/jdbc/OracleDs/value /property /bean ! Datasource that works in any application server You could easily use J。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。