Model Archetype
Database module
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 |
|
Entities module
Datasource connection definition
Edit the src/main/resources/META-INF/persistence.xml file of your module and change the <jta-data-source>osgi:service/jdbc/${project.parent.artifactId}.database.xa</jta-data-source> and the <non-jta-data-source> Sections (referencing the non xa exported one). If you want to disable the transactional access, you’ve also to change the ‘transaction-type’ value to ‘RESOURCE_LOCAL’
Model object definition
In order to define a new model object, you’ve got to extends the ‘AbstractEntity’ abstract class of the src/main/java folder. All Java files that are located on this folder will be ‘delombokized’ e.g they will interpret the lombok annotations and generate the according Java code. Here’s an example of a model entity:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Data | |
// equals, hashcode, getters and setters | |
@Builder | |
// builder pattern | |
@NoArgsConstructor | |
// constructor | |
@AllArgsConstructor | |
// other constructor | |
@Entity | |
// persistence class declaration | |
@XmlRootElement | |
// xml marshalling | |
@EqualsAndHashCode(callSuper = true) | |
@ApiModel( value = "Hello Entity", description = "Hello resource representation" ) | |
public class HelloEntity extends AbstractEntity implements Serializable /* in order to be sendable via JMS*/{ | |
/** | |
* Serial | |
*/ | |
private static final transient long serialVersionUID = 6233801298404301547L; | |
/** | |
* Message | |
*/ | |
@XmlElement | |
// XML node | |
@NotNull(message = "message must not be null") | |
// Validation for null object | |
@Size(min = 2, max = 12, message = "message size must be between 2 and 12") | |
// size validation | |
@Pattern(regexp = "[a-zA-Z0-9]+", message = "must not contain special characters") | |
// pattern validation | |
@ApiModelProperty( value = "The thing to greet", required = true ) | |
private String helloMessage; | |
} |
DAO module
Declaring DAOs
In order to declare a Data Access Object you’ve to expose its API to others via an interface:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JPA accessible interface by business or route module | |
* (see business module for JMS or REST export, don't forget the | |
* osgi.bnd cxf package export). | |
* @author charliemordant | |
*/ | |
public interface HelloRepository extends JpaRepository<HelloEntity, Long> | |
{ | |
/** | |
* Finds by message. | |
* | |
* @param message | |
* message | |
* @return corresponding entities | |
*/ | |
Collection<? extends HelloEntity> findByHelloObjectMessage(String message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OsgiServiceProvider(classes = { HelloRepository.class }) | |
@Singleton | |
@Transactional | |
public class HelloJpaRepository extends DelegatingSimpleJpaRepository<HelloEntity, Long> implements HelloRepository { | |
/** | |
* Entity manager. | |
*/ | |
@PersistenceContext(unitName = "myTestPu") | |
private EntityManager em; | |
@PersistenceUnit( unitName = "myTestPu") | |
private EntityManagerFactory emf; | |
@Override | |
@PostConstruct | |
public void postConstruct() { | |
instanciateDelegateRepository(emf, em, HelloEntity.class); | |
} | |
} |
Adding JMS, REST or integration Tests to the JPA module
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|