hibernate字段注释和生成表的字段顺序和外键名称

开门见山

hibernate4.3.1

都是要修改源码的,我找了好久才找到,hibernate自己内部实现了注释是垮数据库的,只是没有注释的注解


org.hibernate.cfg.Ejb3Column 类copy出来粘贴到项目中修改

bind方法


	public void bind() {
		if ( StringHelper.isNotEmpty( formulaString ) ) {
			LOG.debugf( "Binding formula %s", formulaString );
			formula = new Formula();
			formula.setFormula( formulaString );
		}
		else {
			initMappingColumn(
					logicalColumnName, propertyName, length, precision, scale, nullable, sqlType, unique, true
			);
			if ( defaultValue != null ) {
				mappingColumn.setDefaultValue( defaultValue );
			}
			
			mappingColumn.setComment(comment);
			
			if ( LOG.isDebugEnabled() ) {
				LOG.debugf( "Binding column: %s", toString() );
			}
		}
	}

添加了

mappingColumn.setComment(comment);



	private static void applyColumnDefault(Ejb3Column column, PropertyData inferredData) {
		final XProperty xProperty = inferredData.getProperty();
		if ( xProperty != null ) {
			ColumnDefault columnDefaultAnn = xProperty.getAnnotation( ColumnDefault.class );
			if ( columnDefaultAnn != null ) {
				column.setDefaultValue( columnDefaultAnn.value() );
			}
			ApiModelProperty apip = xProperty.getAnnotation( ApiModelProperty.class );
			column.setComment((apip!=null && apip.value()!=null)?apip.value().replaceAll("'", "\""):"");
		}
		else {
			LOG.trace(
					"Could not perform @ColumnDefault lookup as 'PropertyData' did not give access to XProperty"
			);
		}
	}

添加了

	ApiModelProperty apip = xProperty.getAnnotation( ApiModelProperty.class );
	column.setComment((apip!=null && apip.value()!=null)?apip.value().replaceAll("'", "\""):"");

ApiModelProperty是我的注解,你可以写成你的注解 ,我这是接口文档可以给前端人看,然后数据库也用这里面的描述就省了很多事


外键生成修改,唯一约束名称修改


org.hibernate.mapping.Constraint


	public static String hashedName(String s) {
		return s.replaceAll("`", "_");
//		try {
//			MessageDigest md = MessageDigest.getInstance( "MD5" );
//			md.reset();
//			md.update( s.getBytes() );
//			byte[] digest = md.digest();
//			BigInteger bigInt = new BigInteger( 1, digest );
//			// By converting to base 35 (full alphanumeric), we guarantee
//			// that the length of the name will always be smaller than the 30
//			// character identifier restriction enforced by a few dialects.
//			return bigInt.toString( 35 );
//		}
//		catch ( NoSuchAlgorithmException e ) {
//			throw new HibernateException( "Unable to generate a hashed Constraint name!", e );
//		}
	}


这样生成的外键名称就是 这样的

FK_table_bbs_article_column_bbstype_


表字段数序跟类中定义的顺序一样

org.hibernate.cfg.PropertyContainer


 

private final <span style="background-color: rgb(255, 102, 102);">LinkedHashMap</span><String, XProperty> fieldAccessMap;

	/**
	 * Constains the properties which must be returned in case the class is accessed via {@code AccessType.Property}. Note,
	 * this does not mean that all {@code XProperty}s in this map are properties/methods. Due to JPA access rules single properties
	 * can have different access type than the overall class access type.
	 */
	private final <span style="background-color: rgb(255, 102, 102);">LinkedHashMap</span><String, XProperty> propertyAccessMap;

默认是TreeMap改成LinkedHashMap就可以了

hibernate开发的大神都不看数据库的,所以他们就没考虑这些东西,他们认为用了hibernate就不需要看sql了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值