[Resolvido]Auto Relacionamento JPA + @OneToMany

Pessoal,

Tenho a seguinte classe:

@Entity
@Table(name = "tasks")
public class Task {

     ...

     @OneToMany
     @JoinColumn(name="task_id")
     private List<Task> tasks;

     ...

}

Como podem ver, é uma classe com um relacionamento @OneToMany unidirecional com ela mesma. Mas nos meus testes, a coleção não está sendo carregada. Não dá erro nem nada, apenas não carrega. Quando modo a anotação para

	@OneToMany
	@JoinTable(name = "tasks")
	@JoinColumn(name="task_id")
	private List<Task> subtasks;

ele indica o seguinte erro:


org.hibernate.exception.SQLGrammarException: could not initialize a collection: [br.com.mcastanheira.circinus.model.Task.subtasks#4]
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	at org.hibernate.loader.Loader.loadCollection(Loader.java:2173)
	at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
	at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
	at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
	at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
	at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
	at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:134)
	at org.hibernate.collection.PersistentBag.size(PersistentBag.java:248)
	at java.util.Collections$UnmodifiableCollection.size(Unknown Source)
	at br.com.mcastanheira.circinus.dao.jpa.ProjectDAOJPATest.testGetProjects(ProjectDAOJPATest.java:85)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: SUBTASKS0_.SUBTASKS_ID
	at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
	at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
	at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
	at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
	at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
	at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
	at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
	at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1700)
	at org.hibernate.loader.Loader.doQuery(Loader.java:801)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
	at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
	... 34 more
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: SUBTASKS0_.SUBTASKS_ID
	at org.hsqldb.error.Error.error(Unknown Source)
	at org.hsqldb.error.Error.error(Unknown Source)
	at org.hsqldb.ExpressionColumn.checkColumnsResolved(Unknown Source)
	at org.hsqldb.QueryExpression.resolve(Unknown Source)
	at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
	at org.hsqldb.ParserCommand.compilePart(Unknown Source)
	at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
	at org.hsqldb.Session.compileStatement(Unknown Source)
	at org.hsqldb.StatementManager.compile(Unknown Source)
	at org.hsqldb.Session.execute(Unknown Source)
	... 43 more

Alguém tem alguma sugestão?

Vc tem a tabela do JoinTable? Pelo visto está te faltando a tabela: user lacks privilege or object not found

Cara,

Na verdade, eu não queria criar uma Join Table, mas pelo visto não vou ter outra opção. Mas valeu pela ajuda!

Nesse post eu mostro um @OneToMany, talvez possa te ajudar: JPA @OneToMany e @ManyToOne Unidirecional e Bidirecional e tirar algumas dúvidas.