在Hibernate中正确处理长数据 [英] Properly handling long data in Hibernate

查看:89
本文介绍了在Hibernate中正确处理长数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在休眠时遇到了一个数据太大的问题。即
$ b


引起:java.sql.BatchUpdateException:数据截断:数据过长,第1行'FBZipLoc'列的数据
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1527)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1065)
at org.hibernate.jdbc .BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 12 more


是否可以自动截断所有字段的数据。
或任何其他方式来处理这个问题。
,但我不想检查所有字段的数据长度,因为我有更多的数千字段。

解决方案

我知道Hibernate或MySQL没有办法在没有添加逻辑来处理这个的情况下自动截断字符串。我相信这样的事情不存在的原因是,我永远不会希望我要求插入到数据库中的内容与实际插入的内容不同。



我认为您唯一的选择是...


  1. 更改列定义。使其成为更大的varchar字段或者甚至可以是文本字段。不要花费时间建立一个神奇的工具,只要改变列定义就可以在几次点击中解决这个问题。我推荐你这样做!

  2. 我可以看到你使用某种方面拦截设置器,然后调整设置器的大小如果它大于x长度,则为字符串。这将是最快的为什么在你的代码中处理它。如果更改数据库不是一个选项,并且您有数千个字段,这将是我的下一个选择。


    $ b建立一个String util $ b

    setText(String val){this.text = StringUtil.truncate(val,size);}


    <
    因为你不能真正更新数据库,我会推荐一个方面来拦截String设置器并检查它们的长度,它可能看起来像这样(语法可能关闭,而我没有测试这个)...

    pre $ private $ static final MAX_SIZE_OF_STRINGS = 255;

    @Around(execution(* your.package。*。set *(..))&& args(java.lang.String))
    public void checkAroundSetter( final ProceedingJoinPoint pjp)
    throws Throwable {
    Object [] args = pjp.getArgs(); ()()()()()(){}
    for(int i = 0; i< args.length; i ++){
    if(args [i] instanceof String&&((String)args [i] > MAX_SIZE_OF_STRINGS){
    args [i] =((String)args [i])。subString(0,MAX_SIZE_OF_STRINGS);
    }
    }
    pjp.proceed(args);

    $ / code>

    另外,如果某些图层必须检查已定义的列大小与每个插入表上的所有数据进行比较。


    I am getting a data too large problem in hibernate. that is-

    Caused by: java.sql.BatchUpdateException: Data truncation: Data too long for column 'FBZipLoc' at row 1 at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1527) at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1065) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195) ... 12 more

    Is it possible automatic truncate all field's data. or any other way to handle this problem. but I does not want to check all field's data length because I have more then thousand of fields.

    解决方案

    From what I know there is really no way for Hibernate or MySQL to automatically truncate strings without you adding in the logic to handle this. The reason why I would believe that something like this doesn't exist is that I would never want what I asked to be inserted into a database be different than what really was inserted.

    I think your only options are...

    1. Change the column definitions. Make it a larger varchar field or maybe even a text field. Don't spend time building a magic tool when just changing the column definition would fix this in a couple clicks. I recommend doing this!

    2. I could see you using some sort of aspect to intercept the setters and then adjusting the size of the string if it's larger than x length. This would be the quickest why to handle it in your code. If changing the DB isn't an option and you have thousands of fields this would be my next choice.

    3. Build a String util class which can re-size your strings...

      setText(String val){this.text = StringUtil.truncate(val,size);}

    [UPDATE] Since you can't really update the database I would recommend an aspect to intercept String setters and check their length it might look like this (syntax may be off and i didn't test this)...

    private static final MAX_SIZE_OF_STRINGS = 255;
    
    @Around("execution(* your.package.*.set*(..)) && args(java.lang.String)")
    public void checkAroundSetter(final ProceedingJoinPoint pjp)
        throws Throwable {
        Object[] args = pjp.getArgs();
        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof String && ((String) args[i]).size() > MAX_SIZE_OF_STRINGS) {
                args[i] = ((String)args[i]).subString(0,MAX_SIZE_OF_STRINGS) ;
            }
        }
        pjp.proceed(args);
    }
    

    Also, there would be some additional overhead if some layer had to check the defined column size against all the data coming in to the table on each insert.

    这篇关于在Hibernate中正确处理长数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆