views:

763

answers:

1

I have been struggling with not being able to have the EntityManager be instantiated when I add a OneToMany column to a class that already has a OneToMany column.

Right now the EducationInfo has a OneToOne to user, since each row will be unique to a person, but I want to be able to load all the education information given a username.

I am not changing the orm.xml file when I add the column, I just drop the database so it can be recreated.

class User {
  @Id
  @GeneratedValue(){val strategy = GenerationType.AUTO}
  var id : Int = _

  @Column{val nullable = false, val length=32}
  var firstName : String = ""
  var lastName : String = ""

  @Column{val nullable = false, val unique = true, val length = 30}
  var username : String = ""

  @OneToMany{val fetch = FetchType.EAGER, val cascade=Array(CascadeType.ALL)}
  var address : java.util.List[Address] = new java.util.ArrayList[Address]()

  @OneToMany{val fetch = FetchType.EAGER, val cascade=Array(CascadeType.ALL), val mappedBy="user"}
  var educationInfo : java.util.List[EducationInfo] = new java.util.ArrayList[EducationInfo]()

If I don't include the last column then my unit test works fine, but if I include this then all my unit tests fail.

Here is the class it is trying to use, and the unit tests for this class works fine:

@Entity
@Table{val name="educationinfo"}
class EducationInfo {
  @Id
  @GeneratedValue(){val strategy = GenerationType.AUTO}
  var id : Long = _

  @Column{val nullable = false, val length = 100}
  var name : String = "";

  @OneToOne{val fetch = FetchType.EAGER, val cascade = Array(CascadeType.PERSIST, CascadeType.REMOVE)}
  @JoinColumn{val name = "educationinfo_address_fk", val nullable = false}
  var location : Address = _

  @ManyToOne{val fetch = FetchType.LAZY, val cascade=Array(CascadeType.PERSIST)}
  @JoinColumn{val name = "educationinfo_user_fk", val nullable = false}
  var user : User = _

  @Column{val nullable = false, val length = 100}
  var degree : String = ""

  @Temporal(TemporalType.DATE)
  @Column{val nullable = true}
  var startyear : Date = new Date()
  var endyear : Date = new Date()

  @Column{val nullable = true, val length = 1000}
  var description : String = ""
}

I am also curious if there is some way to know what the error is that prevents the EntityManager from being instantiated, but the main problem is what may be causing a problem when I add this column to User?

UPDATE:

Removed a part that was in error.

UPDATE 2:

I changed the user column in the education entity to be @ManyToOne and all my unit tests failed.

The end of my error log is this:

15035 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
15035 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
15047 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
[PersistenceUnit: jpaweb] Unable to build EntityManagerFactory
[PersistenceUnit: jpaweb] Unable to build EntityManagerFactory

Then EntityManager is null in every test, so it fails.

UPDATE 3:

The Address entity has no foreign keys to anything, as it is called from several entities.

Could this problem be caused by using Apache Derby as the database?

Unfortunately I don't get any error message. I run this from within maven, and though I delete the database each time I can't tell what it happening that is wrong. The @ManyToOne from EducationInfo -> User doesn't cause any problems, but @OneToMany -> EducationInfo from User does.

UPDATE 4:

I added a mappedBy to the User entity as suggested. I had that in another column definition, as I have five definitions that won't work, and I believe they are all for the same reason, so I am just testing with one. Unfortunately it still leads to the EntiyManagerFactory not being built so all the tests fail.

FINAL UPDATE:

The actual case was

cannot simultaneously fetch multiple bags

so I just removed the FetchType completely from EducationInfo and the problem went away.

+3  A: 

OneToMany's complement mapping on the other side of association is ManyToOne, it cannot be OneToOne. That's what's causing EntityManager to fail.

It works when you remove the last line from User because then you're only left with OneToMany to Address and, although not shown here, presumably Address doesn't have a OneToOne link to User.

I'm no Scala expert; in Java you normally get an error and a stack trace printed when EntityManager fails to initialize; error message is not always extremely descriptive but usually says what mapping it has a problem with. Perhaps you need to tweak your logging settings if you don't get that error.

I didn't quite understand your model:

Basically, a user should be able to have several addresses, so I expect it will become a ManyToMany, but each use can also have several instances of education information, so the need for ManyToOne.

That sounds like @OneToMany from User to Address to me and (if "use" meant "user") as @OneToMany from User to EducationInfo.

Right now the EducationInfo has a OneToOne to user, since each row will be unique to a person, but I want to be able to load all the education information given a username.

That conflicts with above. Either you can have several EducationInfo per User or it's "unique to a person".

ChssPly76
Address did have a OneToOne link, but I realized an address could be shared.Thank you for noticing the typo. I will make the change and see what happens.
James Black
It's still the OneToOne on User in EducationInfo. That still needs to be ManyToOne
Jim Barrows
I updated my answer changed to @ManyToOne and it still fails.
James Black
I accepted your answer, you were an incredible help, and the solution isn't something I was expecting.
James Black
Thank you, I actually should have seen that. Scala-style annotations coupled with the fact that I myself never declare collections eager clouded my eyes :( On that topic, there's usually no reason to **always** eagerly fetch collections - and if you need to do it within a specific method you can use 'join fetch' in HQL or override fetch mode if using Criteria.
ChssPly76