Issue
I've a hibernate project working fine (almost). But one relationship with database view isn't working. Somebody could help me ?
Hibernate 5.2 Postgresql 9 Maven Eclipse Apache Tomcat 9
My View
@Entity
@Immutable
public class vclientesgds implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "c_id", updatable = false, nullable = false)
private Long c_id;
@Column
private Long store_id;
@Column
private String cnpj;
@Column
private String company_name;
...
My Class
@Entity
public class EmailGds implements Serializable {
private static final long serialVersionUID = 1L;
public EmailGds() {
System.out.println("E-mails Google Data Studio");
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "emails_gds_seq")
@SequenceGenerator(name = "emails_gds_seq", sequenceName = "emailsgds_id_seq", allocationSize = 1)
@Column
private Long id;
@OneToOne(fetch = FetchType.LAZY, orphanRemoval = false)
@JoinColumn(name = "c_id", nullable = false)
private vclientesgds c_id;
...
When I start the project, I receive the message error:
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@2b843f9b] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate:
alter table if exists napp_front.EmailGds
add constraint FKo824l96t9lt54slsms879qku6
foreign key (c_id)
references napp_front.vclientesgds
ago 06, 2021 6:03:55 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
...
Caused by: org.postgresql.util.PSQLException: ERROR: referenced relation "vclientesgds" is not a table
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:323)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:401)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:322)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:308)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:284)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:279)
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetTo Database.java:54)
... 53 more
I've used @SubSelect too, but without sucess
Solution
Disable the generation of the foreign key:
@JoinColumn(name = "c_id",
foreignKey = @ForeignKey(name = "none",
value = ConstraintMode.NO_CONSTRAINT,
nullable = false))
Answered By - panagiotis