1.JPA中EntityManager不能執行建表語句,提示要加事務Error:javax.persistence.TransactionRequiredException: Executing an update/delete query 換成直接連數據庫原生操做來解決java
2.resources下的文件採用中文命名,讀取不到。redis
3.使用h2數據庫在加了認證後不能訪問h2後臺,登陸進去爲空白f12看到頁面有一個錯誤提示Sorry, Lynx not supported yet 解決方法: 在安全認證配置config文件中的設置過濾不須要認證方法的第一行添加 http.headers().frameOptions().disable();spring
4.錯誤信息:exected single matching bean but found 2: mainDataSourceProperties 使用了多個數據源sql
解決辦法在main函數上配置exclude 數據庫
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
5.JAVA反射強轉建立對象緩存
public <T extends StandardTable> T createStandardTable(Class<T> clz) { StandardTable table = null; try { table = (StandardTable) Class.forName(clz.getName()).newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new SystemException("未取到標準表對象"); } return (T)table; }
6.獲取實體屬性和值安全
/** * 獲取實體屬性 * @param o * @return */ private static String[] getFiledName(Object o) { Field[] fields = o.getClass().getDeclaredFields(); String[] fieldNames = new String[fields.length]; for (int i = 0; i < fields.length; i++) { fieldNames[i] = fields[i].getName(); } return fieldNames; } /** * 根據屬性名獲取屬性值 * @param fieldName * @param o * @return */ private static Object getFieldValueByName(String fieldName, Object o) { try { String firstLetter = fieldName.substring(0, 1).toUpperCase(); String getter = "get" + firstLetter + fieldName.substring(1); Method method = o.getClass().getMethod(getter, new Class[]{}); Object value = method.invoke(o, new Object[]{}); return value; } catch (Exception e) { return null; } }
7.在使用spring boot redis緩存後 錯誤提示 xxx cannot be cast to xxxspringboot
註釋掉下面的引用問題解決session
8.@LastModifiedDate無效app
springboot jpa 中@LastModifiedDate無效的緣由是沒有使用標準save方法進行更新 entityDao.save(e);這樣是有效會自動更新數據庫裏的更新時間字段,自定義hql, sql操做@Query(),entityManager操做均須要本身手動處理
9.hibernate查詢後的對象,set後,更新數據庫的緣由
Hibernate的get和load方法查詢出的實體都是持久化對象,拿到該對象後,若是你調用了該對象的set方法,那麼在事務遞交的時候,Hibernate會把你設置的值自動更新到數據庫中。
解決辦法:
在獲取實體對象後,得到HibernateEntityManager,而後調用得到Session,而後在set完以後用Session的.evict()方法清掉該對象緩存(並不是全部對象緩存)該方法的做用是把持久化對象變成託管狀態。變成託管狀態後,Hibernate就不會再去自動更新該實體。
Hibernate的幾種實體狀態:
1.瞬態:
一個實體經過new操做符建立後,沒有和Hibernate的Session創建關係,也沒有手動賦值過該實體的持久化標識(持久化標識能夠認爲是映射表的主鍵)。
此時該實體中任何屬性的更新都不會反映到數據庫表中。
2.持久化:
當一個實體和Hibernate的Session建立了關係,並獲取了持久化標識,並且在Hibernate的Session生命週期內存在。
此時針對該實體任何屬性的更改都會直接影響到數據庫表中一條記錄對應字段的更新,即與數據庫表同步。
3.脫管:
當一個實體和Hibernate的Session建立了關係,並獲取了持久化標識,而此時Hibernate的Session生命週期結束,實體的持久化標識沒有被改動過。
針對該實體任何屬性的修改都不會及時反映到數據庫表中。
關閉session能夠使實體從持久化狀態轉爲託管狀態。
9.Feign調用報錯The bean 'XXX.FeignClientSpecification', defined in null, could not be registered....的解決辦法
一種方式在application.yml中配置:
spring: main: allow-bean-definition-overriding: true
第二種辦法:
能夠在@FeignClient註解上添加contextId解決該問題,如:@FeignClient(name="common-service", contextId = "exp")
10.統一不返回某些字段 添加註解 前提是採用的是JackSon進行的序列化以及反序列化
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
11.springboot
sessionFactory.getCurrentSession() 異常 springboot jpa org.hibernate.HibernateException: No CurrentSessionContext configured!
添加配置信息 解決
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext