I have the code below in Play for Scala to access a SAP Hana table with Hibernate. I need to implement the same code with MySql, but the problem is that MySql doesn't support sequences (it works with AUTO_INCREMENT columns) and the code breaks because I have to specify @SequenceGenerator
for Hana. Is there a way to compile this code with a condition to exclude the @SequenceGenerator
annotation, so it works for MySql and Hana at the same time?
@Entity
@Table(name = "clients")
class ClientJpa {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
@SequenceGenerator(name="generator", sequenceName = "cliSeq", allocationSize = 1)
var surrogateKey: Int = _
var code: String = _
var name: String = _
}
I think the way to do it is to provide a custom IdGeneratorStrategyInterpreter
and register it using MetadataBuilder.applyIdGenerationTypeInterpreter
. In your custom IdGeneratorStrategyInterpreter
you can override determineGeneratorName
to return "identity"
constant for GenerationType.SEQUENCE
if you know that the code is run against MySql and return null
in all other cases to let the FallbackInterpreter
do its default job (the string "identity"
also comes from FallbackInterpreter.determineGeneratorName
implementation). And you can do nothing in other methods and let the FallbackInterpreter
do it's usual job.
P.S. Please also note that Hibernate's default SequenceStyleGenerator
is actually aware of DBs not supporting "sequences" (exposed via Dialect.supportsSequences
) and is able to emulate similar behavior using additional table. This might or might not be OK for your scenario.
Probably not what you want to hear but AFAIK there's no way to conditionally include annotations. An alternative would be to include the common functionality in a @MappedSuperclass
and inject the concrete instance as appropriate at build time depending on the environment. Something like this:-
@MappedSuperclass
abstract class AbstractClientJpa {
var surrogateKey: Int // abstract
var code: String = _
var name: String = _
}
...
@Entity
@Table(name = "clients")
class HanaClientJpa extends AbstractClientJpa {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
@SequenceGenerator(name="generator", sequenceName = "cliSeq", allocationSize = 1)
var surrogateKey: Int = _
}
...
@Entity
@Table(name = "clients")
class MySQLClientJpa extends AbstractClientJpa {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var surrogateKey: Int = _
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With