Home » Support » Forum » Error: Failed to write value of field X using reflection (oneToMany relation)

Error: Failed to write value of field X using reflection (oneToMany relation)

#1

Hi there! I have been all day trying to solve this. I have read multiple sites and documentation, but I can go through this.

I have a clase named Pedido, that have a reference ManyToOne to a clase Cliente.

@Entity
public class Pedido {
    @Id @GeneratedValue
    private long id;
   
    @Embedded private Direccion origen;
    @ManyToOne(optional=true)
        private Cliente cliente; //Opcional
   
    @Basic(optional=false) private Date fechaYhora; //Fecha en que se debe enviar el coche

}

@Entity
public class Cliente extends Persona{
    @OneToMany(mappedBy="Pedido.cliente")
       public List<Pedido> pedidos;

}

The class Persona, has nothing in special. Just a few variables. String, int, and an embedded class.

The problem is that, when I try to get a list of Client, I get an error.

static public List<Cliente> getClientes() throws Exception{
    em = EMF.createEntityManager();
    List<Cliente> results = null;
    try {
        TypedQuery<Cliente> query = em.createQuery("SELECT c FROM Cliente c", Cliente.class);
        results = query.getResultList();
    } finally {
        em.close();
    }
    return results;
}

 

At the end of this method, "results" is null.Even affter the query. And on em.close() I get an exception:

com.objectdb.o._PersistenceException: Failed to write the value of field field entidades.Cliente.pedidos using reflection

 

What's wrong with this? I suppose i'm making a bad connection on the oneToMany - manyToOne. But I can't figure it out.

Someone can help me?

#2

Please provide a complete test case that demonstrates the exception using the format in these instructions.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

First at all, thank you for answering. And sorry for late answer. I was making a lot of tests. At first the test was working. So I belived it was a mistake on my code. I spended a lot of time searching for it.

But after, I tried a test case without the drop option when creating the database. And there is when the error shows up.

So, the program works fine on the first run. But after that I recieve the "Failed to write the value of field field testodb2.TestODB2$Cliente.pedidos using reflection"

The test case file is adjunct.

text/x-java iconTestODB2.java (2.57KB)
Test case error failed to write using reflection
#4

You have to change the value of the mappedBy attribute:

    @OneToMany(mappedBy="cliente")

i.e. specify the field name on the owner side without the class name.

 

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login