Skip to content

Commit 98d5e94

Browse files
committed
HHH-1872 - Fix Hibernate should handle hbm.auto = update with views
1 parent 0cb00db commit 98d5e94

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaMigratorImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ protected void doMigrationToTargets(
127127
}
128128
checkExportIdentifier( table, exportIdentifiers );
129129
final TableInformation tableInformation = existingDatabase.getTableInformation( table.getQualifiedTableName() );
130+
if ( tableInformation != null && !tableInformation.isPhysicalTable() ) {
131+
continue;
132+
}
130133
if ( tableInformation == null ) {
131134
createTable( table, metadata, targets );
132135
}
@@ -145,6 +148,9 @@ protected void doMigrationToTargets(
145148
// big problem...
146149
throw new SchemaManagementException( "BIG PROBLEM" );
147150
}
151+
if ( !tableInformation.isPhysicalTable() ) {
152+
continue;
153+
}
148154

149155
applyIndexes( table, tableInformation, metadata, targets );
150156
applyUniqueKeys( table, tableInformation, metadata, targets );
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.schemaupdate;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.Id;
11+
import javax.persistence.Index;
12+
import javax.persistence.Table;
13+
14+
import org.hibernate.Query;
15+
import org.hibernate.Session;
16+
import org.hibernate.Transaction;
17+
import org.hibernate.boot.MetadataSources;
18+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
19+
import org.hibernate.boot.spi.MetadataImplementor;
20+
import org.hibernate.cfg.Environment;
21+
import org.hibernate.dialect.PostgreSQL81Dialect;
22+
import org.hibernate.service.ServiceRegistry;
23+
import org.hibernate.tool.hbm2ddl.SchemaExport;
24+
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
25+
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
import org.hibernate.testing.RequiresDialect;
31+
import org.hibernate.testing.TestForIssue;
32+
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
33+
34+
/**
35+
* @author Andrea Boriero
36+
*/
37+
@TestForIssue(jiraKey = "HHH-1872")
38+
@RequiresDialect(PostgreSQL81Dialect.class)
39+
public class SchemaUpdateWithViewsTest extends BaseNonConfigCoreFunctionalTestCase {
40+
protected ServiceRegistry serviceRegistry;
41+
protected MetadataImplementor metadata;
42+
43+
@Test
44+
public void testUpdateSchema() {
45+
SchemaUpdate schemaUpdate = new SchemaUpdate( serviceRegistry, metadata );
46+
schemaUpdate.execute( true, true );
47+
}
48+
49+
@Before
50+
public void setUp() {
51+
createViewWithSameNameOfEntityTable();
52+
serviceRegistry = new StandardServiceRegistryBuilder()
53+
.applySetting( Environment.GLOBALLY_QUOTED_IDENTIFIERS, "false" )
54+
.applySetting( Environment.DEFAULT_SCHEMA, "public" )
55+
.build();
56+
metadata = (MetadataImplementor) new MetadataSources( serviceRegistry )
57+
.addAnnotatedClass( MyEntity.class )
58+
.buildMetadata();
59+
}
60+
61+
private void createViewWithSameNameOfEntityTable() {
62+
Session session = openSession();
63+
Transaction transaction = session.beginTransaction();
64+
Query query = session.createSQLQuery( "CREATE VIEW MyEntity AS SELECT 'Hello World' " );
65+
query.executeUpdate();
66+
transaction.commit();
67+
session.close();
68+
}
69+
70+
@After
71+
public void tearDown() {
72+
dropView();
73+
System.out.println( "********* Starting SchemaExport (drop) for TEAR-DOWN *************************" );
74+
SchemaExport schemaExport = new SchemaExport( serviceRegistry, metadata );
75+
schemaExport.drop( true, true );
76+
System.out.println( "********* Completed SchemaExport (drop) for TEAR-DOWN *************************" );
77+
78+
StandardServiceRegistryBuilder.destroy( serviceRegistry );
79+
serviceRegistry = null;
80+
}
81+
82+
private void dropView() {
83+
Session session = openSession();
84+
Transaction transaction = session.beginTransaction();
85+
Query query = session.createSQLQuery( "DROP VIEW MyEntity " );
86+
query.executeUpdate();
87+
transaction.commit();
88+
session.close();
89+
}
90+
91+
92+
@Entity
93+
@Table(name = "MyEntity", indexes = {@Index(columnList = "id", name = "user_id_hidx")})
94+
public static class MyEntity {
95+
private int id;
96+
97+
@Id
98+
public int getId() {
99+
return this.id;
100+
}
101+
102+
public void setId(final int id) {
103+
this.id = id;
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)