Java code causes persistence problem

Asked

Viewed 89 times

0

After adding the following lines to my Metadicas.java class, a persistence problem arose that was not there before:

Lines added:

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn
private List<Integer> notas;

Error:

>javax.persistence.PersistenceException: [PersistenceUnit: h2PersistenceUnit] Unable to build EntityManagerFactory
 org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924)
 org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899)
 org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59)
 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
 javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
 play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:38)
 play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:91)
 play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:91)
 scala.collection.immutable.List.foreach(List.scala:383)
 play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:91)
 play.api.Play$$anonfun$start$1.apply(Play.scala:91)
 play.api.Play$$anonfun$start$1.apply(Play.scala:91)
 play.utils.Threads$.withContextClassLoader(Threads.scala:21)
 play.api.Play$.start(Play.scala:90)
 play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:157)
 play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:130)
 scala.Option.map(Option.scala:145)
 play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:130)
 play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:128)
 scala.util.Success.flatMap(Try.scala:230)
 play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:128)
 play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:120)
 scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
 scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
 scala.concurrent.forkjoin.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1361)
 scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
 scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
 scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
 scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

I tried many ways but I couldn’t fix it at all.

  • Erick, I don’t know what you intend to do, but you can’t join with a list of integers like that. Like me, Hibernate won’t guess where he’s getting that list from. The normal way to do this is to have a list of some other entity.

2 answers

1

You must specify more the column that Voce wants to join. For example:

@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "web_usuario_role", joinColumns = { @JoinColumn(name = "id_usuario", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "id_role", referencedColumnName = "id") })
private List<Papel> authorities;

I don’t think it’s possible to merge with an Integer List.

0

What the

ti.sof002

said it is correct. However, be very careful with Fetchtype.EAGER and Fetchtype.Lazy. this may cause some bumps for you. (slow system, error "Lazyinitializeexception, Nullpointer", etc.)

When you need to bring a populated list, it is more performatico popular to list with a namedQuery.

/* Não é recomentado usar EAGER em mapeamentos OneToMany, pois quando este objeto for recuperado vai trazer a lista desse objeto e seus sub-objetos todos populados, issso pode deixar o sistema lento. Por padrão um mapeamento OneToMany já é Lazy, mas pode colocar para facilitar a leitura do código.*/
@OneToMany(fetch = FetchType.LAZY)     
@JoinTable(name = "web_usuario_role", joinColumns = { @JoinColumn(name = "id_usuario", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "id_role", referencedColumnName = "id") })
private List<Papel> authorities;

Hug comments and criticisms are welcome :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.