Java Persistance API (JPA)
Project Dependencies
Add the following dependency to your pom.xml/build.gradle file:
implementation("com.raynigon.unit-api:spring-boot-jpa-starter:3.0.5")
<dependency>
<groupId>com.raynigon.unit-api</groupId>
<artifactId>spring-boot-jpa-starter</artifactId>
<version>3.0.5</version>
</dependency>
Limitations
Currently the Raynigon Unit API JPA integration only works for hibernate.
Usage
To use the Raynigon Unit API with JPA, an Entity needs to be created.
This Entity needs to have a @Type
annotation with a definition for the QuantityType
.
Warning
You need to have the @Type
annotation on every Entity due to a missing feature in hibernate
(see here).
@Entity
@Table("basic_entity")
public class BasicEntity {
@Id
@Column(name="id")
public String id;
@JpaUnit(KilometrePerHour.class)
@Type(QuantityType.class)
@Column(name="speed")
public Quantity<Speed> speed;
}
Depending on the database you are using, you need to specify the columnDefinition
property in the @Column
annotation.
By default, the value stored in the database will be a floating-point value. In this example the value will be saved in "km/h".