1

What's the methodology of testing @SequenceGenerator from Hibernate? I want to be sure that every sequence is perfectly mapped, no mistake in spelling, and incrementing is done by 1. Is there any way to do this dynamically for all sequences?

Here's the sample of my sequence mapping:

@Column(name = "ADDRESS_ID", nullable = false, precision = 20)
@Id
@SequenceGenerator(name = "AddressSeq", sequenceName = "ADDRESS_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AddressSeq")
private Long addressId;
Arturo Volpe
  • 3,442
  • 3
  • 25
  • 40
shx
  • 1,068
  • 1
  • 14
  • 30
  • You want to create a In memory database? Hibernate creates the sequences? or is a external database? you can use reflection to get all sequences names, and query the database, but I don't think it is a unit test. – Arturo Volpe Aug 27 '14 at 21:56
  • Inmemory but Oracle compatibility mode with schema preloaded from script. You can take a look on project at https://github.com/slavisah/mybusiness and review it with few words. Thank you – shx Aug 27 '14 at 22:01
  • 1
    I think you don't need to test if the sequence is incremented by 1, you need some level of trust in hibernate, but for the sequence name, you can use reflection to get all mapped classes, get they id's and get the name of the sequence, to compare it with your raw sql file, if the `CREATE SEQUENCE` exists, then it must be rigth. – Arturo Volpe Aug 27 '14 at 22:06
  • Do you have any example of that kind of reflection? – shx Aug 28 '14 at 03:31
  • 1
    I add a pull request to your project. – Arturo Volpe Aug 28 '14 at 12:11
  • Can you check this question I posted. Same repo. http://stackoverflow.com/questions/25535991/hibernate-manytoone-mapping-automatic-loading-without-id-property-set – shx Aug 28 '14 at 18:06

1 Answers1

1

You can use this answer to obtain a list of your entity classes:

    List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
    classLoadersList.add(ClasspathHelper.contextClassLoader());
    classLoadersList.add(ClasspathHelper.staticClassLoader());

    reflections = new Reflections(
            new ConfigurationBuilder()
                    .setScanners(new SubTypesScanner(false),
                            new ResourcesScanner())
                    .setUrls(
                            ClasspathHelper.forClassLoader(classLoadersList
                                    .toArray(new ClassLoader[0])))
                    .filterInputsBy(
                            new FilterBuilder().include(FilterBuilder
                                    .prefix("me.business.model"))));

And get your DDL with:

    ClassPathResource cpr = new ClassPathResource("db/schema.sql");
    schemaContent = new String(FileCopyUtils.copyToByteArray(cpr
            .getInputStream())).toLowerCase();

And get the sequence for each class witH;

private String getSequenceName(Class<?> clazz) {
    for (Field f : clazz.getDeclaredFields()) {
        if (f.isAnnotationPresent(SequenceGenerator.class)) {
            SequenceGenerator sg = f.getAnnotation(SequenceGenerator.class);
            return sg.sequenceName();
        }
    }
    return null;
}

The test is simple:

    Set<Class<?>> entities = reflections.getSubTypesOf(Object.class);
    for (Class<?> clazz : entities) {
        String name = getSequenceName(clazz);
        if (name == null)
            continue;
        if (!schemaContent.contains(name.toLowerCase())) {
            fail("The clazz " + clazz.getSimpleName()
                    + " has a sequence called: " + name
                    + " and it doesn't exits");
        }
    }

You can see it here

If you want to see if it works, change the sequence name in one of your entitites and run the test.

Community
  • 1
  • 1
Arturo Volpe
  • 3,442
  • 3
  • 25
  • 40
  • Not working. Reflection can't find classes from given package. List is empty in test method. Probably some security issues – shx Aug 28 '14 at 18:01
  • How do you execute this? I test in my computer with ecilpse before the pull request. – Arturo Volpe Aug 28 '14 at 18:02
  • I ran it in Netbeans, will try it in Eclipse also. Probably it's because of the JVM version. – shx Aug 28 '14 at 18:08
  • Execute it with `mvn package` or `mvn test` and it will scan everthing, if you execute `mvn -Dtest=me.business.dao.hbn.CheckSequenceNames test` the classes are not loaded, It's not a IDE problem, see the executed classpath, it is not complete. – Arturo Volpe Aug 28 '14 at 18:11
  • Now I have it running but always with some [exception](https://github.com/slavisah/mybusiness/blob/master/src/test/resources/errors/exceptionWhenRunningCheckSequenceNames.txt) thrown at startup. That's some kind of warning because test was completed without that. Can you get the point where's the problem? – shx Aug 28 '14 at 20:17
  • Please copy the exception to pastebin and share here. Or email me, you can see my email in the commits. – Arturo Volpe Aug 28 '14 at 20:32
  • you can check it at the link hidden under the exception word up in my comment. it's on git repo under the /error folder in test resources. – shx Aug 28 '14 at 21:24
  • I don't know why and what means this exceptions. see [this](https://code.google.com/p/reflections/issues/detail?id=148) – Arturo Volpe Aug 28 '14 at 22:58