Fixefid is a java library wich permits to define a fixed fields text record with Java Bean or Java Enum. Often a text record must be retrieved from data persisted on a database. Or a text record must be persisted on a database.
The solution is to define a mapping from the record’s fields with the persistence model. If the persistance layer is realized with JPA Entities, the mapping can be done directly to the JPA Entity with the Fixefid annotations. Infact a JPA Entity is a POJO, that’s a Java Bean. And so, we can annotate the JPA Entity with the Fixefid annotations to realize the mapping, without the need to create two models, one for the record and another one for the JPA Entity.
The environment is as a follows:
- Java 8
- Spring Boot 2.3.4.RELEASE
- Spring Data JPA
- Fixefid 1.1.0
- H2 Database
For example we can have a Customer bean like this one:
@Entity
@FixefidRecord
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@FixefidField(fieldLen = 19, fieldOrdinal = 0, fieldType = FieldType.N)
private Long id;
@FixefidField(fieldLen = 50, fieldOrdinal = 1, fieldType = FieldType.AN)
private String firstName;
@FixefidField(fieldLen = 50, fieldOrdinal = 2, fieldType = FieldType.AN)
private String lastName;
protected Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}
public Long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
The Customer above is annotated with Entity and FixefidRecord. The fields are annotated with FixefidField and other JPA annotations. To obtain the record from the database:
Customer customer = repository.findById(1L);
String record = new BeanRecord(customer).toString();
To save the record to the database:
String newRecord = "0000000000000000001Paul Robinson ";
Customer newCustomer = new Customer();
new BeanRecord(newCustomer, newRecord);
repository.save(newCustomer);
I made a video tutorial where the example above is explained in the detail way.
Here the project of the example on github.