1

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?

2 Answers 2

2

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.