I'm working on the following code:
Product
@Entity
@Table(name = "products",
public class Product extends RepresentativeModel {
@ManyToMany(mappedBy = "products", targetEntity = Category.class)
private Set<Category> categories = new HashSet<>();
}
Category
@Entity
@Table(name = "categories",
public class Category extends RepresentativeModel {
@ManyToMany
@JoinTable(name = "productToCategory",
joinColumns = {@JoinColumn(name = "categoryId")},
inverseJoinColumns = {@JoinColumn(name = "productId")})
private Set<Product> products = new HashSet<>();
}
EM config
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Qualifier("dataSource") DataSource source) {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(source);
entityManagerFactory.setPackagesToScan("com.x.model");
JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
entityManagerFactory.setJpaProperties(jpaProperties());
entityManagerFactory.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
log.debug("PERSISTENCE UNIT " + PERSISTENCE_UNIT_NAME);
return entityManagerFactory;
}
Package info
package is com.x.model
for both
Properties properties = new Properties();
properties.put("hibernate.default_schema", "public");
entityManagerFactory.setJpaProperties(properties);
Problem
Products tables exists in the database, but I'm getting this error message:
org.postgresql.util.PSQLException: ERROR: relation "products" does not exist
I tried to change entity name to " products " or to change mappedBy
to " Product ", but without success.
How to fix this?
I've had the same problem, and all the answers in internet did not help me. And I find out that postrgres creates tables in case sensitive manner, if you add double quotes when creating tables.
"auteur" and auteur are two different names.
I don't know exactly how it works, but apparently Hibernate cannot find the @JoinTable
for the @ManyToMany
relation, because, as I wrote above, this is a different table for postgres. Knowing this solved my problem. Maybe this will help someone else.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.