Mapping BLOBs & CLOBs with JPA and Hibernate

Sdílet
Vložit
  • čas přidán 2. 09. 2019
  • Today I will show you how to map BLOBs and CLOBs with JPA and Hibernate. You will learn how to map these database columns to a String or a byte[] and how to use the java.sql classes Blob and Clob. And at the end of this video, I will show you an easy and portable way to load your BLOBs and CLOBs lazily.
    Especially the lazy-loading trick is a mapping that most developers are surprised about and that I have used with great success in various client projects.
    But before we start, if this is your first time here, and you want to learn how to create your entity mappings with ease, build incredible efficient persistence layers with Hibernate and all types of other Java persistence related stuff, start now by subscribing and clicking the bell, so you don't miss anything.
    Databases use the data types binary large object aka BLOB and character large object aka CLOB to store large objects, like images and very long texts. JPA and Hibernate provide two kinds of mappings for these types.
    You can choose if you want to:
    Materialize the LOB and map it to a byte[] or a String. This mapping is defined by the JPA specification and prioritizes ease of use over performance.
    Or you can use JDBC’s LOB locators java.sql.Blob and java.sql.Clob. The LOB locators enable your JDBC driver to optimize for performance, e.g., by streaming the data. This mapping is Hibernate-specific.
    If you like this video, please give me your thumbs up and share it with your friends and co-workers.
    Like my channel? Subscribe!
    ➜ bit.ly/2cUsid8
    Join the free Member Library:
    goo.gl/dtyIIC
    Read the accompanying post: thoughts-on-java.org/mapping-...
    Want to connect with me?
    Blog: www.thoughts-on-java.org/
    Twitter: / thjanssen123
    Facebook: / thoughtsonjava
    #JPA #Hibernate #blob #clob
  • Věda a technologie

Komentáře • 12

  • @mohammedhussein1717
    @mohammedhussein1717 Před 3 lety

    Thank you and well presented.

  • @lbreuss
    @lbreuss Před 4 lety

    Thanks for this nice video.
    2:15 Mapping a LOB to String or byte[]
    3:51 Mapping LOB to java.sql.Clob or Blob. Hibernate specific mapping, requires usage of BlobProxy.
    5:56 Lazy loading of LOBs. To "guarantee lazy loading" of the String / byte[] approach, put LOB to separate entity, @OneToOne @MapsId, and on demand manually navigate to the subentity containing the LOB.

  • @CHIP_X
    @CHIP_X Před 3 lety

    Very usefull, thx - Muy util, gracias

  • @SushilKumarBhaskar
    @SushilKumarBhaskar Před 4 lety +2

    Greatest one

  • @nandakishorems9654
    @nandakishorems9654 Před 3 lety

    Awesome Video

  • @jvrlol5104
    @jvrlol5104 Před rokem

    Gracias por tu video crak

  • @JonathanMiranda
    @JonathanMiranda Před rokem

    Hi, what about BFILES type column from Oracle, how to map that type on entities or there are workarounds? ... thanks !

  • @riteshch.roymoulick8901

    Your BlobProxy.generateProxy() is not working for me with IBM db2 database. Earlier I have used Hibernate.createBlob() and it was working fine with hibernate 3. But recently I have updated my hibernate version to 5.4.18. I can't get any alternative of Hibernate.createBlob() in hibernate 5. I am getting Sqlcode=-302 error while persisting blob data into database. But with the same data older code Hibernate.createBlob() is working absolutely fine. Please advise.

  • @adbyte
    @adbyte Před 4 lety +1

    Could you share some mybatis tutorials please?

  • @prakashtayade1972
    @prakashtayade1972 Před 3 lety

    Sir, can you teach security approaches in Hibernate?

  • @davidsalleyezonme1283
    @davidsalleyezonme1283 Před 4 lety

    Make a tuturial how to save img into a database

  • @hangofox1720
    @hangofox1720 Před rokem +1

    Good night. I have an Oracle database with a table with 4 Clob-type fields. The CRUD (Read, Modify and Delete) works correctly except for the create shows me the following error when I use Postman. How is the correct syntax in Spring to insert clob type data without showing me the following error?:
    023-06-13 19:21:15.353 ERROR 6556 --- [nio-8084-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
    oracle.jdbc.OracleDatabaseException: ORA-00932: tipos de dato inconsistentes: se esperaba - se ha obtenido CLOB.