# How to handle @Lob properties when using 2 different databases with Hibernate

**URL:** https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171
**Category:** Hibernate ORM
**Created:** [January 30, 2019, 4:30am UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171 "2019-01-30T04:30:26Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![cwhite](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cwhite](https://discourse.hibernate.org/u/cwhite)
#### Post date: [January 30, 2019, 4:30am UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171/1 "2019-01-30T04:30:26Z")

</div>

We have a legacy application using Derby that we want to now work with both PostgreSQL and Derby.  
Everything works fine on both except for this CLOB handling that needs to be different for this one class…  
Switching the annotations as follows works, but we want one build for either DB.

**For Derby**

```auto
@MappedSuperclass
public class AbstractXmlLobBean extends StandardRootBean {
	@Lob
	@Column(name = "marshalledXml")
	private String xml;

```

**For PostgreSQL** (we need it to use ‘text’)

```auto
@MappedSuperclass
public class AbstractXmlLobBean extends StandardRootBean {
	@Lob
	@Type(type = "org.hibernate.type.TextType")
	@Column(name = "marshalledXml")
	private String xml;

```

How can we accomplish this (using either annotations / mapping files / programatically)?

This is using Hibernate 3.6.9.  
Thanks!

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [January 30, 2019, 8:30am UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171/2 "2019-01-30T08:30:04Z")

</div>

So how do the `@Lob` handling differs between the two?

---

<div class="post-metadata">

### Author: ![cwhite](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cwhite](https://discourse.hibernate.org/u/cwhite)
#### Post date: [January 30, 2019, 1:44pm UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171/3 "2019-01-30T13:44:47Z")

</div>

For Postgres, I need to have:

`@Type(type = "org.hibernate.type.TextType")`

In order for it to use ‘text’ in the database.

But I can NOT have that annotation for Derby otherwise the LOBs will not work.

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [January 30, 2019, 4:17pm UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171/4 "2019-01-30T16:17:44Z")

</div>

You can use the annotation by default, and override the mapping using XML for Derby.

Check out [this article for more details](https://vladmihalcea.com/how-to-replace-the-table-identifier-generator-with-either-sequence-or-identity-in-a-portable-way/).

---

<div class="post-metadata">

### Author: ![cwhite](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cwhite](https://discourse.hibernate.org/u/cwhite)
#### Post date: [January 30, 2019, 9:52pm UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171/5 "2019-01-30T21:52:09Z")

</div>

Thanks, I’m trying but not sure how to do the xml.

Should I be using **entity-mappings** like in the page you sent? If so, how do I register those into hibernate config?

Or should I be using **hibernate-mapping** in which case I’m not sure what the xml should look like.

Our hibernate configuration code looks like this… (trying to add the xml resource)

```auto
	protected Configuration createHibernateConfiguration() {
		final Configuration cfg = new Configuration();
		cfg.configure(this.tasDbConfig.getHibernateConfigResource());

		// Entities
		for (Class<?> aClass : getEntityClasses()) {
			cfg.addAnnotatedClass(aClass);
		}
		if( this.tasDbConfig.getDbType() == DefaultTASDBConfig.DatabaseType.Derby ) {
			cfg.addResource("AbstractXMLLobBean-derby.hbm.xml");
		}
		cfg.setInterceptor(new MyInterceptor());
		return cfg;
	}

```

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [January 30, 2019, 10:57pm UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171/6 "2019-01-30T22:57:52Z")

</div>

You can use the Hibernate legacy mapping too.

---

<div class="post-metadata">

### Author: ![cwhite](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cwhite](https://discourse.hibernate.org/u/cwhite)
#### Post date: [January 30, 2019, 11:00pm UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171/7 "2019-01-30T23:00:24Z")

</div>

So what would the XML (Hibernate 3.6.9) look like to be the equivalent of these annotations?

```auto
@MappedSuperclass
public class AbstractXmlLobBean extends StandardRootBean {
	@Lob
	@Column(name = "marshalledXml")
	private String xml;

```

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [January 30, 2019, 11:58pm UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171/8 "2019-01-30T23:58:45Z")

</div>

There wouldn’t be any equivalent in HBM for MappedSuperclass. You can use the Hibernate 3 documentation to find what was available back then. HBM mappings are deprecated anyway, so you are better off migrating to Hibernate 5 in the long run.

---

<div class="post-metadata">

### Author: ![cwhite](https://avatars.discourse-cdn.com/v4/letter/c/c6cbf5/32.png) [@cwhite](https://discourse.hibernate.org/u/cwhite)
#### Post date: [January 31, 2019, 1:05am UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171/9 "2019-01-31T01:05:03Z")

</div>

Migrating to Hibernate 5 is sort of out of the question. Like I said, this is an existing legacy app that needs to run on Postgres (cause it outgrew Derby on one install).

If I were to use the entity-mappings like you suggest, how do I register that xml mapping into the Hibernate configuration?

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [January 31, 2019, 6:33am UTC](https://discourse.hibernate.org/t/how-to-handle-lob-properties-when-using-2-different-databases-with-hibernate/2171/10 "2019-01-31T06:33:25Z")

</div>

You can use the `addResource` method of the `Configuration` object.

However, you need to pass the relative path of the XML file as it’s located on the classptah. To see how that works, just add a [breakpoint in this Hibernate test](https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/test/java/org/hibernate/test/extralazy/ExtraLazyTest.java#L43) and run the test in debug mode.
