使用SerialBlob与byte [] [英] Using a SerialBlob vs byte[]

查看:124
本文介绍了使用SerialBlob与byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用hibernate来存储和检索MySQL数据库中的数据.我使用的是字节数组,但是遇到了SerialBlob类.我可以成功使用该类,但似乎找不到使用SerialBlob和字节数组之间的任何区别.有人知道您希望使用byte []的SerialBlob inlue的基本区别或可能的情况吗?

I am using hibernate to store and retrieve data from a MySQL database. I was using a byte array but came across the SerialBlob class. I can use the class successfully but I cant seem to find any difference between using the SerialBlob and a byte array. Does anyone know the basic differences or possible situations you wish to use a SerialBlob inlue of a byte[] are?

推荐答案

您是对的,SerialBlob只是围绕byte[]的一个抽象,但是:

You are right that the SerialBlob is just a thin abstraction around a byte[], but:

  • 您是否在团队中工作?
  • 有时您会犯错误吗?
  • 您懒于写评论吗?
  • 有时您会忘记一年前代码的实际作用吗?

如果通过解决以上任何问题,则可能应使用

If you anwsered any of the above questions with a yes, you should probably use SerialBlob.

与围绕简单数据结构(例如,想想ByteBuffer)或其他类的任何其他抽象基本相同.您想在byte[]上使用它,因为:

It's basically the same with any other abstraction around a simple data structure (think ByteBuffer, for example) or another class. You want to use it over byte[], because:

  1. 更具描述性. byte[]可能是某种缓存,可能是循环缓冲区,也可能是某种完整性检查机制出了问题.但是,如果使用SerialBlob,显然这只是来自数据库/的二进制数据的一小块,将存储在数据库中.

  1. It's more descriptive. A byte[] could be some sort of cache, it could be a circular buffer, it could be some sort of integrity checking mechanism gone wrong. But if you use SerialBlob, it's obvious that this is just a blob of binary data from the database / to be stored in the database.

您可以在类上使用方法,而不是手动进行数组处理,如果您不知道代码,该方法也更易于阅读.您的代码读者也必须理解甚至是琐碎的数组操作.具有好名字的方法是自我描述的.

Instead of manual array handling, you use methods on the class, which is, again, easier to read if you don't know the code. Even trivial array manipulation must be comprehended by the reader of your code. A method with a good name is self-descriptive.

这对您的队友以及一年后阅读此代码的人都有帮助.

This is helpful for your teammates and also for you when you'll read this code in a year.

更能证明错误.每次您编写任何新代码时,都有很大的机会在其中编写一个错误.起初它可能不可见,但可能在其中. SerialBlob代码已经过世界各地成千上万的人的测试,可以肯定地说您不会遇到任何与之相关的错误.

It's more error proof. Every time you write any new code, there's a good chance you had made a bug in it. It may be not visible at first, but it is probably in there. The SerialBlob code has been tested by thousands of people around the world and it's safe to say that you won't get any bugs associated to it.

即使您确定自己的字节数组处理正确,因为它是如此简单,如果其他人在半年内找到您的代码并开始优化"呢?如果他重用了旧的Blob,或者弄乱了您的魔术数组填充怎么办?

Even if you're sure you got your byte array handling right, because it's so straightforward, what if somebody else finds your code in half a year and starts "optimizing" things? What if he reuses an old blob, or messes up with your magic array padding? Every single off-by-one error in index manipulating will corrupt your data and that might not be detected right away (You are writing unit tests, aren't you?).

它将您限制为只有少数几种可能的交互方式.这实际上看起来像一个缺点,但事实并非如此!它确保您在使用完blob后不会将其用作本地临时变量.它确保您不会尝试使用String或其他任何愚蠢的东西.它确保您仅将其用作Blob.再次,清晰和安全.

It restricts you to only a handful of possible interactions. This might actually look like a demerit, but it's not! It ensures you won't be using your blob as a local temporary variable after you're done with it. It ensures you won't try to make a String out of it or anything silly. It makes sure you'll only use it as a blob. Again, clarity and safety.

它已经被编写,并且看起来总是一样的.您不必为每个项目编写新的实现,也不必在十个不同的项目中阅读十个不同的实现.如果您在任何人的项目中都看到SerialBlob,用法将对您很清楚.每个人都使用同一个.

It's already written and always looks the same. You don't have to write a new implementation for every project, or read ten different implementations in ten different projects. If you'll ever see a SerialBlob in anyone's project, the usage will be clear to you. Everyone uses the same one.


TL; DR:几年前(或者也许仍然在C语言中),可以使用byte[].在Java(通常是OOP)中,尝试使用为作业设计的特定类,而不是原始的(低级)结构,因为它可以更清晰地描述您的意图,减少错误并从长远来看减少代码长度


TL; DR: A few years ago (or maybe still in C), using a byte[] would be ok. In Java (and OOP in general), try to use a specific class designed for the job instead of a primitive (low level) structure as it more clearly describes your intents, produces less errors and reduces the length of your code in the long run.

这篇关于使用SerialBlob与byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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