views:

38

answers:

2

Hi all

When trying unit tests with Spring Security & Hibernate, none of the security entities "user" or "authorities" are being autocreated. What I have done so far is to write an "user" bo that triggers generation of the appropiate table. However, I am stuck with the authorities:

(as advised by http://java.dzone.com/articles/getting-started-spring for postgresql)

  CREATE TABLE authorities
  (
    username character varying(50) NOT NULL,
    authority character varying(50) NOT NULL,
    CONSTRAINT fk_authorities_users FOREIGN KEY (username)
        REFERENCES users (username) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION
  );
  1. Question: With Hibernate/JPA2, what is the appropiate syntax in order to create a BO representing this query?

  2. Question: Actually, I do not want to create the entry using my own BO. Any better way to make Spring Security or Hibernate create all required tables during test run?

Thanks

+1  A: 

Set the hibernate property hibernate.hbm2ddl.auto to update, for example. This should let hibernate automatically create (and update) the tables in needs.

<property name="hibernate.hbm2ddl.auto" value="update" />
Bozho
Hi. Thanks for the advice. However, my 1. Question refers to the BO code - I don't know how to create the contraint. Example?
+1  A: 

Actually, I do not want to create the entry using my own BO. Any better way to make Spring Security or Hibernate create all required tables during test run?

If you don't plan to use Hibernate to interact with these tables, it makes indeed little sense to have Entities for them.

My suggestion would thus be to place the Spring Security tables creation script in an import.sql file and to put this file on the root of the class path and Hibernate will automatically execute it after schema export. See Spring/Hibernate testing: Inserting test data after DDL creation for details (just put your DDL statements on a single line).


Thanks, Pascal, this is just what I have been looking for, however, it does not work. I use maven and put import.sql into the resources dir root (content: CREATE TABLE justatest (aaa character varying(50) NOT NULL );). I also set . Running mvn test copies import.sql to target dir... but nothing happens. logback[debug] does not mention import.sql at all. Any idea where I am going wrong? (Hibernate V 3.5.1-Final)

I'm using this feature with Maven and I cannot reproduce your problem. I have hbm2ddl.auto set to create, my import.sql file is in src/test/resources and it gets executed as expected at the end of the schema export when running tests. Here is the log entry I get (using logback):

20:44:37.949 [main] INFO  o.h.tool.hbm2ddl.SchemaExport - Executing import script: /import.sql
Pascal Thivent
Thanks, Pascal, this is just what I have been looking for, however, it does not work. I use maven and put import.sql into the resources dir root (content: CREATE TABLE justatest (aaa character varying(50) NOT NULL );). I also set <property name="hibernate.hbm2ddl.auto" value="create" />. Running mvn test copies import.sql to target dir... but nothing happens. logback[debug] does not mention import.sql at all. Any idea where I am going wrong? (Hibernate V 3.5.1-Final)
Hi, Pascal, thanks again. Unfortunately, I can't get it working. Never mind anyway. I am working on a Windows station and I shall give it another try on a Linux machine just to make sure it has nothing to do with the filesystem. I know, it shouldn't have anyway, but I have encountered most curiouse things in my life ...