JPA 自动生成表字段排序不正确

网上一堆直接修改treeMap为LinkedHashMap就可以了

我试了一下,发现不行,然后发现他们都是3.X版本的(经过我看5.X的源码,我严重怀疑网上这个方法其实也不行的)

我的是5.X版本的

然后我就去看了源码,发现这不怪hibernate

因为hibernate使用反射机制,而反射机制取得的顺序是无序的

(无序的原因是这,而不是hibernate无序,可能hibernate知道没法解决,所以干脆直接使用无序的treeMap,其实treeMap是有序的,根据字母排序,网上说无序是扯淡吧,百度的东西真不可靠)

解决方法应该也是有的,只不过我不想这样做,所以没试!

下面就讲一下我的思路:

给实体类的字段添加排序用的注解

然后重写org.hibernate.cfg.PropertyContainer,在通过反射取得字段字后,再根据注解排序

代价太大,我放弃了!等我要看数据库表,在用工具排序吧。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用JPA生成MYSQL数据库表时,可以设置表的默认排序规则。MYSQL的默认排序规则取决于服务器的配置,一般情况下,MYSQL的默认排序规则是utf8mb4_general_ci。 要设置JPA生成MYSQL数据库表的排序规则,可以在application.properties或application.yml文件中添加以下配置: ``` spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8mb4&serverTimezone=Asia/Shanghai&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true spring.jpa.properties.hibernate.hbm2ddl.charset=utf8mb4 spring.jpa.properties.hibernate.connection.CharSet=utf8mb4 spring.jpa.properties.hibernate.connection.useUnicode=true spring.jpa.properties.hibernate.connection.characterEncoding=utf-8 ``` 其中,spring.jpa.database-platform和spring.jpa.properties.hibernate.dialect都指定了使用的数据库方言,这里使用的是MySQL8Dialect。spring.jpa.properties.hibernate.connection.CharSet、spring.jpa.properties.hibernate.connection.useUnicode和spring.jpa.properties.hibernate.connection.characterEncoding指定了数据库的字符编码和字符集。spring.jpa.properties.hibernate.hbm2ddl.charset指定了DDL生成文件的编码,这里也设置为utf8mb4。最后,需要在数据库连接字符串中添加useUnicode=true和characterEncoding=utf8mb4,确保JPA能够正确地处理UTF-8编码的字符。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值