Androider
Androider

Reputation: 2804

Automatically Insert first row soon after table is created / recreated using Hibernate

I want Username = Administrator and Password = admin, soon after table is created (or whenever table is recreate).

  1. Is there any way to insert first row using following code. I do not want to use a separate insert query
  2. Is there any constraint in Hibernate to restrict user from deleting (first) row

    @Entity
    @Table(name = "Users")
    public class Users implements Serializable {
    
        @Id
        @Column(name = "Username", unique = true, nullable = false)
        private String username;
    
        @Column(name = "Password", nullable = false)
        private String password;
    
        /**************** getter & setter ************/
    
    }
    

Upvotes: 0

Views: 757

Answers (1)

maksimov
maksimov

Reputation: 5811

What you seem to be looking for is called fixtures. With Hibernate you can supply import.sql file on your classpath with your initial data (insert statements).

There's a bit more information on the JBoss' site.

The import.sql file is a simple text file with SQL statements, one line per statement:

insert into users (id, username) values (1, 'administrator');
insert into users (id, username) values (2, 'user');
...

Upvotes: 2

Related Questions