Persisting an entity on deployment

I'm currently developing a web application for creating and running simulations. For this application, I want the user to be able to load some set presets and use these. For this to work, I want the presets to be persisted in the database on deployment, but I can't seem to find a fitting way to do this.

For the project I'm using Wildfly with Hibernate. I know I can make a import.sql to do what I want, but this is not optimal for my use, seeing that the preset-entity have a lot of attributes that are much more easily set as a .java file.

Here is an excerpt of my Simulation.java class:

@javax.persistence.Entity
public class Simulation
{
/**
 * An automatically generated id
 */
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;

/**
 * The simulation name, limited to 255 characters
 */
@NotBlank
@Length(max = 255)
private String name;

/**
 * Date the simulation was created
 */
@Temporal(TemporalType.TIMESTAMP)
private Date date;

/**
 * Result of the simulation
 */
@OneToOne(cascade = {CascadeType.ALL})
private SimulationResult result;

@Transient
@OneToMany(cascade=CascadeType.ALL)
private List<Consumer> consumers;

@Transient
@OneToMany(cascade=CascadeType.ALL)
private List<Producer> producers;

private int ticks;

private boolean preset;
[...]

How can I make an instance of this and persist it on deployment?

Upvotes: 1

Views: 62

Answers (2)

Knarf
Knarf

Reputation: 1273

You can create an startup class:

@Singleton
@Startup
public class StartupBean {

   @PostConstruct
   private void startup() { ... }

}

Inside of the startup method you can create the presets.

Upvotes: 0

Aparna Chaudhary
Aparna Chaudhary

Reputation: 1335

You can cosider using WildFly db-bootstrap extension https://github.com/wildfly-extras/db-bootstrap/blob/master/README.md

Upvotes: 2

Related Questions