How to enter records when starting the JSF+JPA system?

Asked

Viewed 143 times

3

How do I insert pre-registered records into the system with jsf and jpa. I want when the system starts the admin user to be created.

  • Why not use SQL scripts? i think it’s a bad idea to do this, very likely that you will only run once, imagine that you are creating something within the application that needs to always be checking if it was created, you could even use a tool to manage these scripts as flyway or liquibase where you can get control of versioning scripts, it’s just a hint, hugging.

  • As I am using JPA, I would have to access the system and then go back to the data bandwidth server to run the script

3 answers

3

You can use the @Postconstruct annotation in your managedBean method. This annotation will cause the method to be invoked after your mBean is instantiated.

Example:

@ManagedBean
@RequestScoped
public class TesteMBean{

    private UserAdm userAdm;

    @PostConstruct
    public void init(){
          userAdm = new UserAdm();
    }

    public UserAdm getUserAdm() {
        return userAdm;
    }

    public void setUserAdm(UserAdm userAdm) {
        this.userAdm = userAdm;
    }
}

0

Another option is to implement a Servletcontextlistener and insert the records into the contextInitialized method, which is invoked when the server initializes the application context. This way you don’t need to access the application for this first action (insert the records) to be executed.

And Servletcontextlistener is managed by the application server, that is, it is possible to access resources such as Entitymanager.

@Inject
private EntityManager em;

Also, if you use a Managed Bean JSF, the method annotated with @Postconstruct will run every time the corresponding jsf page is loaded, while the contextInitialized method will only be loaded once, when the server starts the application.

@Override
public void contextInitialized(ServletContextEvent event) {

    StringBuilder sb = new StringBuilder();
    sb.append("Aplicação xyz iniciada: ");
    sb.append(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(Calendar.getInstance().getTime()));
    System.out.println(sb.toString());

    gravarRegistrosPreCadastrados(); //TODO
}
  • The question is how I run this sql on my system.

0

Daniel, complementing what Paulo Cardoso said, you can use @Postconstruct on a particular Managed Bean and apply @Applicationscoped, so it will run only once when you access the application.

@Named(value = "tarefasBean")
@ApplicationScoped
public class TarefasBean implements Serializable {

public TarefasBean() {
    System.out.println("INICIANDO TAREFAS BEAM");
}

@PostConstruct
public void executarTarefas() {
    verificarConexão();
    iniciarQuartz();
}

private void iniciarQuartz() {
   //iniciando agenda de tarefas aki
}

public void verificarConexão() {
    //meu codigo aki
}

}

In the method checks connection, if there is a problem, the user will be redirected, if everything is right, I start my "task schedule", and this will only happen the first access of the application. From there, you can do your inserts and things like in the posconstruct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.