- 浏览: 76567 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
zhuzf:
我也出现这样的问题,但我的问题是:查询的语句中的字段与实体中的 ...
java.sql.SQLException: ORA-01722: 无效数字
进入:http://www.hibernate.org 说明文档: 英文:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/ 中文:http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/ 下载:hibernate annotation 3.4.0 GA 得到:hibernate-annotations.jar hibernate-commons-annotation.jar ejb3-persistence.jar 数据库:mysql category表:id,name,description <Pk>id product表:id,name ,price, description ,category_id <pk>id <fk>category_id 新建java project项目: Add Hibernate Capabilities hibernate.cfg.xml 代码; <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3307/users </property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="myeclipse.connection.profile"> mysqlusers </property> <property name="format_sql">true</property> <property name="show_sql">true</property> <property name="current_session_context_class">thread</property> <mapping class="com.b510.examples.Product" /> <mapping class="com.b510.examples.Category" /> </session-factory> </hibernate-configuration> 利用Hibernate的逆向工程生成: Category.java and Product.java Category.java package com.b510.examples; import java.util.HashSet; import java.util.Set; // 标准注解 import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; //增加的注解 import org.hibernate.annotations.GenericGenerator; //当前的类是一个持久化类,是Category这个类。他映射了一个表category。所对应的 数据库是users //这句:@Table(name = "category", catalog = "users") 可以省略 @Entity @Table(name = "category", catalog = "users") public class Category implements java.io.Serializable { private static final long serialVersionUID = 3240281547213597385L; private Integer id; private String name; private String description; private Set<Product> products = new HashSet<Product>(0); public Category() { } public Category(String name, String description, Set<Product> products) { this.name = name; this.description = description; this.products = products; } // 主键 :@Id 主键生成方式:strategy = "increment" //映射表中id这个字段,不能为空,并且是唯一的 @GenericGenerator(name = "generator", strategy = "increment") @Id @GeneratedValue(generator = "generator") @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } //映射表中name这个字段 ,长度是500 @Column(name = "name", length = 500) public String getName() { return this.name; } public void setName(String name) { this.name = name; } //映射表中description这个字段 ,长度是500 @Column(name = "description", length = 500) public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } //级联操作:cascade = CascadeType.ALL //延迟加载:fetch = FetchType.LAZY //映射:mappedBy = "category" //一对多方式 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "category") public Set<Product> getProducts() { return this.products; } public void setProducts(Set<Product> products) { this.products = products; } } Product.java 代码: package com.b510.examples; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name = "product", catalog = "users") public class Product implements java.io.Serializable { private static final long serialVersionUID = -1546206493725028472L; private Integer id; private Category category; private String name; private String price; private String descripton; public Product() { } public Product(Category category, String name, String price, String descripton) { this.category = category; this.name = name; this.price = price; this.descripton = descripton; } @GenericGenerator(name = "generator", strategy = "increment") @Id @GeneratedValue(generator = "generator") @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } //延迟加载:多对一方式 //关联信息:外键name = "category_id" @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "category_id") public Category getCategory() { return this.category; } public void setCategory(Category category) { this.category = category; } @Column(name = "name", length = 500) public String getName() { return this.name; } public void setName(String name) { this.name = name; } @Column(name = "price", length = 10) public String getPrice() { return this.price; } public void setPrice(String price) { this.price = price; } @Column(name = "descripton", length = 500) public String getDescripton() { return this.descripton; } public void setDescripton(String descripton) { this.descripton = descripton; } } 测试代码: HibernateTest.java 代码: /** * */ package com.b510.examples; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; /** * * @author XHW * * @date 2011-7-20 * */ public class HibernateTest { public static void main(String[] args) { HibernateTest test=new HibernateTest(); test.add(); test.find(); } public void add(){ Configuration config=new AnnotationConfiguration(); config.configure(); SessionFactory sessionFactory=config.buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); Category c=(Category)session.get(Category.class, 5); Product p=new Product(); p.setName("计算机科学与技术"); p.setPrice("123"); p.setDescripton("计算机科学与技术,好啊,真是红啊"); p.setCategory(c); c.getProducts().add(p); session.save(p); session.getTransaction().commit(); } public void find(){ Configuration config=new AnnotationConfiguration(); config.configure(); SessionFactory sessionFactory=config.buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); Category c=(Category)session.get(Category.class, 5); System.out.println("id: "+c.getId()+" name:"+c.getName()); Set<Product> p=c.getProducts(); for(Product product:p){ System.out.println("id:"+product.getId()+" name:"+product.getName()+" description:"+product.getDescripton()); } session.getTransaction().commit(); } } 运行效果: log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version). log4j:WARN Please initialize the log4j system properly. Hibernate: select category0_.id as id1_0_, category0_.description as descript2_1_0_, category0_.name as name1_0_ from users.category category0_ where category0_.id=? Hibernate: select products0_.category_id as category5_1_, products0_.id as id1_, products0_.id as id0_0_, products0_.category_id as category5_0_0_, products0_.descripton as descripton0_0_, products0_.name as name0_0_, products0_.price as price0_0_ from users.product products0_ where products0_.category_id=? Hibernate: select max(id) from product Hibernate: insert into users.product (category_id, descripton, name, price, id) values (?, ?, ?, ?, ?) Hibernate: select category0_.id as id5_0_, category0_.description as descript2_5_0_, category0_.name as name5_0_ from users.category category0_ where category0_.id=? id: 5 name:xml33 Hibernate: select products0_.category_id as category5_1_, products0_.id as id1_, products0_.id as id4_0_, products0_.category_id as category5_4_0_, products0_.descripton as descripton4_0_, products0_.name as name4_0_, products0_.price as price4_0_ from users.product products0_ where products0_.category_id=? id:9 name:计算机科学与技术 description:计算机科学与技术,好啊,真是红啊
发表评论
-
Hibernate中execute、executeQuery和executeUpdate之间的区别
2012-05-03 18:23 976Statement 接口提供了三种执行 SQL 语句的方法:e ... -
java.sql.SQLException: ORA-01722: 无效数字
2011-12-15 13:58 12824[eStore]2011-12-15 13:47:21,890 ... -
org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException
2011-11-24 16:15 1802org.springframework.orm.hiberna ... -
hibernate中SQLQuery
2011-11-21 17:46 1149对原生SQL查询执行的控制是通过S ...
相关推荐
使用hibernate注解,必须要使用库hibernate-commons-annotations,hibernate-core,hibernate-jpa,ejb3-persistence,javassist等
hibernate注解说明文档,hibernate注解说明文档
Hibernate注解用法总结
Hibernate 注解的说明文档,有pdf和html.工具类文档
Hibernate注解API.pdfHibernate注解API.pdfHibernate注解API.pdf
Hibernate注解jar包 介绍:Hibernate Annotations注解使用的jar包。
Hibernate注解大全
Hibernate注解详解(超全面不解释).pdf
hibernate注解详解
hibernate 注解 一对多,多对一,多对多,注解解释
hibernate注解配置,有喜欢的朋友可以下载
ssh框架构建 hibernate spring都使用注解
springmvc+hibernate框架整合 demo 都是注解完成
Spring+Hibernate注解声明式事务实例
hibernate注解中英文版 hibernate注解中英文版 hibernate注解中英文版 hibernate注解中英文版
hibernate 注解 annotation 教程
* @GenericGenerator —— 注解声明了一个hibernate的主键生成策略。支持十三种策略。该注解有如下属性 * name 指定生成器名称 * strategy 指定具体生成器的类名(指定生成策略)。 * parameters 得到strategy指定的...
hibernate注解开发所需的jar包 包括 ejb3-persistance.jar、hibernate-commons-annotations.jar、hibernate-annotations.jar
一。实体Bean 每个持久化POJO类都是一个实体Bean, 通过在类的定义中使用 @Entity 注解来进行声明。...Hibernate 可以对类的属性或者方法进行注解。属性对应field类别,方法的 getXxx()对应property类别。