415 words

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

#1
2017-02-25 00:59

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?

elnll002
elnll002's picture
Joined on 2017-02-24
User Post #2
#2
2017-02-27 03:11

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)
support
support's picture
Joined on 2010-05-03
User Post #2,708
#3
2017-03-02 03:59

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.

elnll002
elnll002's picture
Joined on 2017-02-24
User Post #3
#4
2017-03-09 13:44

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)
support
support's picture
Joined on 2010-05-03
User Post #2,710

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel