Platinum Partner
java,jpa,tips and tricks

JPA's Nasty "Unknown abstract schema type" Error

I'd been trying to debug the following error for days, and it drove me crazy. The problem, in a nutshell, is that JPA refuses to compile one of my NamedQueries, throwing the following error:

Error compiling the query [UserVO.findByUserName: SELECT u FROM UserVO u WHERE u.name = :name].
Unknown abstract schema type [UserVO]


After numerous Google searches, I concluded that JPA will throw the "Unknown abstract schema type"  error when JPA fails to locate your entity class.  Most often, this type of error occurs when:

  • You have provided the database table name instead of the entity class name in the JPA query. For example, if you have an entity class called "UserVO", which maps to the table name "users", the query "SELECT u from users u" will throw the above exception.

  • When running JPA in standalone mode, or not in a Java EE container (such as Tomcat 5 or 6), you forget to explicitly list all entity classes in the persistence.xml file, thus causing JPA to fail to locate the entities when compiling the query.


Neither of above applied to my case. I have explicitly listed all my entity classes in the persistence.xml and I am sure my JPA query is valid. I have tested my code with different JPA implementations, but always saw the same error.

Here's my UserVO class:
@Entity(name = "users")
@NamedQuery(name = "UserVO.findByUserName",
query = "SELECT u FROM UserVO u WHERE u.name = :name")
public class UserVO extends BaseVO implements Serializable {

...
...
}


If I remove the NamedQuery, my JPA works as expected, i.e, I am able to insert, delete, and update the UserVO object.

Now, to all my smart readers, can you spot what's wrong in my code? Think about it and then scroll down for the answer...

 

 

 

 

 

 

 

 

Answer: The culprit is the Entity annotation. I explicitly named the UserVO entity "users". JPA has no problem to map the UserVO entity to the users database table. However, JPA has a problem when compiling the JPA Query: it can't find the UserVO entity in the JPA context because I have renamed the UserVO entity to users.

To resolve this, just add a @Table annotation with the table name, as shown in the code below:
@Entity
@Table(name = "users")
@NamedQuery(name = "UserVO.findByUserName",
query = "SELECT u FROM UserVO u WHERE u.name = :name")
public class UserVO extends BaseVO implements Serializable {

...
...
}

Haha, stupid me... Anyway, Happy New Year to everyone.
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}