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了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章