0
votes

I'm having trouble with JPA. When I try to persist a new entity I will get the following error:

java.lang.IllegalArgumentException: Object: Technology is not a known entity type.

Technology is the title of the Category class. (Note: my code has worked before!) So I decided to drop all tables in the database (PostgreSql)

N̶o̶w̶ ̶I̶ ̶h̶a̶v̶e̶ ̶t̶h̶e̶ ̶f̶o̶l̶l̶o̶w̶i̶n̶g̶ ̶e̶r̶r̶o̶r̶:̶

(I've had this error before, but it dissapeared after a while)
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.postgresql.util.PSQLException: ERROR: relation "category" does not exist Position: 47 Error Code: 0 Call: SELECT ID, DESCRIPTION, TITLE, PARENT_ID FROM category Query: ReadAllQuery(referenceClass=Category sql="SELECT ID, DESCRIPTION, TITLE, PARENT_ID FROM category")

Entity class (Category.Java)

@Entity
public class Category implements DataObject, Serializable {

@Id
@GeneratedValue 
private Long id;
private String title;
private String description;

@ManyToOne @JsonIgnore 
private Category parent;

@Transient
private Long parentId;
//functions
}

persistence.xml

  <persistence-unit name="BloggerPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>blogdb</jta-data-source>
    <class>domain.Blog</class>
    <class>domain.BlogUser</class>
    <class>domain.Category</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
    </properties>
  </persistence-unit>

Edit

I solved the relation ERROR by restarting the glassfish server. Now I'm back at the first error.

DatabaseJPA.java

public class DatabaseJPA<T extends DataObject> implements Database<T> {

private EntityManagerFactory factory;
private EntityManager manager;
private Class<T> type;

public DatabaseJPA(String name, Class<T> type)
{
      factory = Persistence.createEntityManagerFactory(name);
      manager = factory.createEntityManager();
      this.type = type;
}
@Override
public T ReadValue(long id) {

   return (T) manager.find(type, id);
}

@Override
public void InsertValue(T value) {


    manager.getTransaction().begin();

    manager.persist(value);

    manager.flush();
    manager.getTransaction().commit();

}

@Override
public void UpdateValue(long id, T value) {
    manager.getTransaction().begin();
    manager.merge(value);
    manager.flush();
    manager.getTransaction().commit();
}


@Override
public void DeleteValue(long id) {
    manager.getTransaction().begin();
    manager.remove(this.ReadValue(id));
    manager.flush();
    manager.getTransaction().commit();
}

@Override
public void close() throws IOException {
    factory.close();
    manager.close();
}
}
2
Well maybe I don't understand something but if you don't have any tables in your db then what do you expect when you call select from category if your table called category doesn't exist? - wawek
Yes but JPA should automatically generate the tables based on the properties of the Entity - Svexo
Can you show how you are trying to persist entity causing a problem? - wawek
I've added my db class - Svexo
Ok, so now show us how you are trying to call your InsertValue method - what you are passing there. - wawek

2 Answers

0
votes

If you have relation problem again:

@ManyToOne
private A parent;
@OneToMany(mappedBy="parent")
private Collection<A> children;

For the IllegalArgumentException can you show us a portion of code where you call your persist and it raises the exception ?

0
votes

Changing the transaction-type to LOCAL_RESOURCE in persistence.xml solved the issue and restarting the glassfish server.