- 浏览: 288619 次
- 性别:
文章分类
- 全部博客 (105)
- windows (6)
- spring (5)
- hibernate (6)
- log4j (3)
- html (8)
- Servlet (2)
- mysql (4)
- java (18)
- tomcat (5)
- 数据库 (4)
- eclipse (5)
- css (2)
- word (1)
- javascript (8)
- 手机 (5)
- 日志 (1)
- linux (9)
- ssh (1)
- 线程 (1)
- dom (2)
- 算法 (1)
- android (4)
- xp (1)
- http (4)
- web (3)
- 生活 (6)
- oracle (9)
- shell (4)
- plsql (2)
- ubuntu (9)
- 网络配置 (1)
- 编辑器 (2)
- C (1)
- C++ (2)
- cygwin (1)
- CDT (2)
- ios (1)
- g++ (1)
- gcc (2)
- 魔方 (1)
- chrome (2)
- 购物 (1)
- 游戏 (1)
- 模拟器 (1)
- weblogic (3)
- OSGi (1)
- transaction (2)
- fusioncharts (1)
- jta (1)
- 加密 (1)
- RSA (1)
- jBPM (2)
- jboss (1)
- wildfly (1)
- 电子书 (1)
- example code (1)
- redis (1)
- jemalloc (1)
- libc (1)
- sokect (1)
- nio (1)
- office (1)
- elastic-job (1)
- zookeeper (1)
- quartz (1)
- webservice (3)
- axis (1)
- CentOS7 (1)
- VM (1)
- hbase (3)
- maven (1)
- 硬件 (1)
- 单片机 (1)
- 电路图 (1)
- axis2 (1)
- jaxws (2)
- vpn (1)
- pptp (1)
- CKFinder (1)
- utf-8 (1)
- jdk (2)
- tail (1)
- cmd (2)
- srvany (1)
- rktool (1)
- python (1)
- tensorflow (1)
- 字符编码 (2)
- wget (1)
- ftp (1)
- jsp (0)
- nginx (1)
- openlayer (1)
- GEO (1)
- geojson (1)
- wgs84经纬度坐标系 (1)
- wgs84 墨卡托投影坐标系 (1)
- 连接池 (1)
- jdbc (1)
- druid (1)
- 文档 (0)
- iso week (1)
- date (1)
- golang (0)
- vscode (0)
- fiber (0)
最新评论
-
cybersnow:
恩不错,谢谢
The 'manifest_version' key must be present and set to 2 (without quotes). -
LinApex:
这样效率很低哦
在js、css中嵌入java/jsp代码 -
bnmnba:
369485270 写道楼主在吗 你这个注解方式根本就不行 ...
hibernate 获取实体的表名、主键名、列名 -
黄浦江:
Mysql 5.6.5 noInstall 版本下没有my.i ...
Can't connect to MySQL server on 'localhost' (10061) -
369485270:
楼主在吗 你这个注解方式根本就不行 报错啊 org.hibe ...
hibernate 获取实体的表名、主键名、列名
修改自网络:http://blog.csdn.net/fhwbj/article/details/3267787
import java.util.Iterator; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.hibernate.mapping.Column; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.PrimaryKey; import org.hibernate.mapping.Property; /** * 功能描述:根据实体类得到对应的表名、主键名、字段名工具类 * </p> * 注:po类名须与对应映射文件名一致,即Student.java与Student.hbm.xml<br> * //修改为主注解方式,此hbm文件已经不需要 * * * @Date:Nov 10, 2008 * @Time:3:13:07 PM * */ public class EntityUtil { private static Configuration hibernateConf; private static Configuration getHibernateConf() { if (hibernateConf == null) { // hibernateConf=new Configuration();//*.hbm.xml方式 hibernateConf=new AnnotationConfiguration().configure();//注解方式 hibernateConf.buildSessionFactory();//注解方式必须的 } return hibernateConf; } private static PersistentClass getPersistentClass(Class<?> clazz) { synchronized (EntityUtil.class) { PersistentClass pc = getHibernateConf().getClassMapping( clazz.getName()); if (pc == null) { // hibernateConf = getHibernateConf().addClass(clazz);//*.hbm.xml方式 pc = getHibernateConf().getClassMapping(clazz.getName()); } return pc; } } /** * 功能描述:获取实体对应的表名 * * @param clazz * 实体类 * @return 表名 */ public static String getTableName(Class<?> clazz) { return getPersistentClass(clazz).getTable().getName(); } /** * 功能描述:获取实体对应表的主键字段名称,只适用于唯一主键的情况 * * @param clazz * 实体类 * @return 主键字段名称 */ public static String getPrimaryKey(Class<?> clazz) { return getPrimaryKeys(clazz).getColumn(0).getName(); } /** * 功能描述:获取实体对应表的主键字段名称 * * @param clazz * 实体类 * @return 主键对象primaryKey ,可用primaryKey.getColumn(i).getName() */ public static PrimaryKey getPrimaryKeys(Class<?> clazz) { return getPersistentClass(clazz).getTable().getPrimaryKey(); } /** * 功能描述:通过实体类和属性,获取实体类属性对应的表字段名称 * * @param clazz * 实体类 * @param propertyName * 属性名称 * @return 字段名称 */ public static String getColumnName(Class<?> clazz, String propertyName) { PersistentClass persistentClass = getPersistentClass(clazz); Property property = persistentClass.getProperty(propertyName); Iterator<?> it = property.getColumnIterator(); if (it.hasNext()) { Column column = (Column) it.next(); return column.getName(); } return null; } }
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"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/managerdb</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/shop</property> <property name="connection.username">root</property> <property name="connection.password">ynb</property> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> --> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">10</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <!-- <mapping package="pp.entity"/> --> <mapping class="pp.entity.Department"/> <mapping class="pp.entity.Element"/> <mapping class="pp.entity.Employee"/> <mapping class="pp.entity.EmployeeType"/> <mapping class="pp.entity.Goods"/> <mapping class="pp.entity.GoodsType"/> <mapping class="pp.entity.GoodsFactoryType"/> <mapping class="pp.entity.ProductAssembleRecord"/> <mapping class="pp.entity.PersonalInfo"/> <mapping class="pp.entity.Product"/> <mapping class="pp.entity.SalaryType"/> <mapping class="pp.entity.Storage"/> <!-- <mapping class="pp.entity.StoreroomKeeper"/> --> <mapping class="pp.entity.CheckRecord"/> <mapping class="pp.entity.CheckRecordType"/> <mapping class="pp.entity.EncourageRecord"/> <mapping class="pp.entity.EncourageRecordType"/> <mapping class="pp.entity.MonthSalaryRecord"/> <mapping class="pp.entity.MonthSalaryType"/> <mapping class="pp.entity.MonthSalary"/> <mapping class="pp.entity.StorageRecord"/> <mapping class="pp.entity.TradeRecord"/> <mapping class="pp.entity.TradeRecordType"/> <mapping class="pp.entity.FundRecordType"/> <mapping class="pp.entity.FundRecord"/> <mapping class="pp.entity.Fund"/> <mapping class="pp.entity.SalaryCareInfo"/> <mapping class="pp.entity.StaticInfo"/> <mapping class="pp.entity.SaleAchievementTakePercent"/> <mapping class="pp.entity.ProductionRecord"/> <!-- <mapping class="pp.entity.EmployeeProductionRecord"/> <mapping class="pp.entity.DepartmentProductionRecord"/> --> <mapping class="pp.entity.ProductionRecordType"/> <mapping class="pp.entity.TestEntity"/> </session-factory> </hibernate-configuration>
评论
2 楼
bnmnba
2012-04-19
369485270 写道
楼主在吗 你这个注解方式根本就不行 报错啊 org.hibernate.HibernateException: /hibernate.cfg.xml not found 晕死 很急啊 能加个QQ 帮我看看嘛 谢谢了 QQ369485270
呵呵,忘了说明了,hibernate.cfg.xml文件是必须的。
1 楼
369485270
2012-03-20
楼主在吗 你这个注解方式根本就不行 报错啊 org.hibernate.HibernateException: /hibernate.cfg.xml not found 晕死 很急啊 能加个QQ 帮我看看嘛 谢谢了 QQ369485270
发表评论
-
hibernate报错:Cannot add or update a child row: a foreign key constraint fails
2012-02-12 21:40 14118我遇到这个问题的原因是:把主键作为外键关联到了其他表的主键。 ... -
hibernate+mysql float类型无法插入小数
2012-02-09 16:43 2628往数据库里面插入小数,结果被四舍五入为整数。当时看见这个情况让 ... -
Hibernate3的DetachedCriteria支持
2012-02-07 17:47 915转:http://dev.yesky.com/241/2033 ... -
hibernate获得实体的主键、表明、列名
2012-01-15 17:13 0转:http://blog.csdn.net/fhwb ... -
hibernate报错:Use of @OneToMany or @ManyToMany targeting an unmapped class:
2012-01-15 10:47 4360报错如下: org.hibernate.Annotat ... -
Hibernate @Column(s) not allowed on a @ManyToOne property
2012-01-15 02:16 9433在写一个自关联的实体时报错:org.hibernate.Ann ...
相关推荐
hibernate中自动生成主键的办法
hibernate 无主键表映射资源说明: 1. 简单Demo 2. Demo对应的sql语句 3. 对应博客文档
Hibernate用UUID作为主键的Demo,采用Java默认的方法,来生成对应的ID后,存储到数据库中
下面小编就为大家带来一篇spring hibernate实现动态替换表名(分表)的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
关于Hibernate的各种主键生成策略与配置详解
hibernate一对一主键关联代码实现
Hibernate一对一主键映射实例,里面包含源程序,测试程序,映射说明
hibernate一对一主键关联(注解版)
hibernate中用到联合主键的看看就会了很棒的资料
这个是针对初学Hibernate一对多主键关联映射的源代码 ,希望对初学者有所帮助,开发环境是MyEclipse7.5和HIbernate3.2
博文链接:https://llying.iteye.com/blog/220802
demo代码; 博客地址:http://blog.csdn.net/smilefyx/article/details/48951933
hibernate的联合主键怎么设置、hibernate的联合主键怎么设置.doc
联合主键用Hibernate注解映射方式主要有三种: 第一、将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将该类注解为@Embeddable,最后在主类中(该类不包含联合主键...
如果使用Hibernate开发legacy的数据库应用,对于数据库表中有使用字符串作为主键或者使用复合主键情况,那么对于这些情况的影射档是比较麻烦的。该示例应用演示了两张表ITEM和CATEGORY_ITEM表有主外键关系,并且ITEM...
Hibernate各种主键生成策略与配置详解 1、assigned 主键由外部程序负责生成,在 save() 之前必须指定一个。Hibernate不负责维护主键生成。与Hibernate和底层数据库都无关,可以跨数据库。在存储对象前,必须要使用...
hibernate复合主键设置,里面很全面的配置,可以拿到直接用
NULL 博文链接:https://liuna718-163-com.iteye.com/blog/748089
Hibernate联合主键,MyEclipse的例子
hibernate的主键生成策略介绍,详细介绍了有关hibernate的常用主键生成策略,希望对一些正在学习hibernate的同学有一定的帮助