views:

367

answers:

2

I have the following java class:

package domain;

//imports

@Entity
public class User {

    @Id @GeneratedValue
    private long id;

    private String name;
    private String password;
    private String mail;
    //Getters, Setters and Constructors
}

When I change the file extension to .groovy, the application stops working. In fact it throws this stacktrace:

org.springframework.dao.InvalidDataAccessApiUsageException: Unknown entity: domain.User; nested exception is java.lang.IllegalArgumentException: Unknown entity: domain.User

I'm reading this book and the author states that any groovy class can take the place of a java class just changing its extension. So why does spring and JPA don't recognize my groovy class?

Has anyone used this technologies successfully?

A: 

Both Groovy (.groovy) and Java (.java) source files will compile to bytecode (.class) files. There's no file extension renaming necessary.

If you have groovy code, you need to use the groovy compiler to generate a .class file, much like a .java file will generate a .class file with javac.

It doesn't seem to be a JPA or Spring issue.

Rob Beardow
It seems to compile it because the class that uses the User.groovy, compiles cleanly. I do have to change the source file extension if I move from java to groovy as far as I know.
Pablo Fernandez
True, Rob was just saying that changing the extension was irrelevant (though necessary), since Spring would be using the compiled result (.class file in both cases).
Bill James
+2  A: 

There's not a lot of info here, so time to guess.

Seems like Spring can't find the User.class file. The Groovy and Java compilers may have different output paths, and the Groovy output path may not be in your classpath, or your deployment set.

Check your War, is User.class in it? Is it in WEB-INF/classes/domain? If not, then the Groovy output folder doesn't seem to be in your deployment. Try changing the Groovy output folder to match your Java output folder.

Bill James