Domanda

I have an entity in src/test/java written just for tests. It seems that IntelliJ IDEA doesn't recognize it. When I'm attempting to write a query Intellij doesn't recognize the class as an entity, I can't write "select * from " - it marks the class as nonexistent.

I have tried mapping the class directly in persistence.xml with but that doesn't work as well (IntlliJ still doesn't recognize it and in persistence.xml I have to mark it as plain text or IntelliJ will mark it as error as well).

Also, in all the entities that I have in src/main I can see the symbol that it is mapped:

However the one in src/test is not:

EDIT:

I am using JPA facet, not Hibernate (which requires hibernate.cfg.xml and not persistence.xml), and it allows only one persistence.xml file to be added

È stato utile?

Soluzione 2

Figured it out and answering for posterity:

As @dozortsev-anton mentioned, we need another xml file in src/test/resources - but instead of another persistence.xml with different persistence units, we need an actual orm.xml to map the entity:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
                 version="2.0">

    <entity class="my.wonderful.class">
        <table name="TABLE_NAME" />
    </entity>

</entity-mappings>

Note that just writing the <entity> node (without the table element inside) is sufficient for IntelliJ IDEA to recognize the class as an entity and start auto-completing queries where this class is used. However, the column names will not be recognized:

(upon hovering you'll get the "Cannot resolve column..." error). The table element will fix this - by mapping the entity to the table (the same table as you have in the @Table annotation) all errors will be gone.

Altri suggerimenti

For recognize you need set IDEA Facets for scr/test folder. Go to Project Structure -> Facets and in Descriptions set path to persistence.xml:

enter image description here

But you don't need create entity for test. You should make test cases for entities that stored in src/main/java/... folder.

Update:

enter image description here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top