Error when trying to map a Set type object (Proper

2019-08-21 15:18发布

问题:

In order to accomplish another task, I need redefine my pojos classes and use property access to take advantage of JavaFX properties in my mentioned classes, but I'm facing this error.

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: deposito, for columns: [org.hibernate.mapping.Column(productoses)]

I have already tryied the solutions mentioned in org.hibernate.MappingException: Could not determine type for: java.util.Set and org.hibernate.MappingException: Could not determine type for: java.util.List but still can't make it work.

Here is my OneToMany entity class and here is my ManyToOne class.

And this is the stacktrace.

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: deposito, for columns: [org.hibernate.mapping.Column(productoses)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:455)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:422)
    at org.hibernate.mapping.Property.isValid(Property.java:226)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:597)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:451)
    at org.hibernate.boot.internal.MetadataImpl.buildSessionFactory(MetadataImpl.java:170)
    at ajfmo.inventario.utils.HibernateUtil.getSessionFactory(HibernateUtil.java:19)
    at ajfmo.inventario.DAO.ProductDAO.<init>(ProductDAO.java:20)
    at ajfmo.inventario.view.MainView.<init>(MainView.java:60)

Edit


This is my HibernateUtil class. This one is the DAO that appears in the stack trace.


Thank you in advance, this is my first project using hibernate... Or my first project at all indeed.

回答1:

In your Deposito class I see that your JPA annotation is on the property and not on the getter like everywhere else in the class (I doubt this is your problem, but maybe a good idea to be consistent)

And here you have:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "deposito")
private Set<Productos> productoses = new HashSet<Productos>(0);

public Set<Productos> getProductoses() {
  return this.productoses;
}

In the Productos class you have:

private ObjectProperty<Deposito> deposito;
private Deposito _deposito;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "deposito_producto", referencedColumnName = "descripcion_deposito", nullable = false)
public Deposito getDeposito() {
  if (deposito == null) {
    return _deposito;
  } else {
    return deposito.get();
  }
}

referencedColumnName = "descripcion_deposito" point to this method (which is probably not the one you intended):

@Column(name = "descripcion_deposito", unique = true, nullable = false, length = 45)
public String getDescripcionDeposito() {
  if (descripcionDeposito == null) {
    return _descripcionDeposito;
  } else {
    return descripcionDeposito.get();
  }
} 

I'm not sure exactly what your column names are, but the referencedColumnName should point to the primary/foreign key of the field of the Productos object, try this in the Productos class:

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "deposito_producto", referencedColumnName = "idDeposito", nullable = false)
public Deposito getDeposito() {
  if (deposito == null) {
    return _deposito;
  } else {
    return deposito.get();
  }
}