[Switch] JPA (Hibernate) does not generate foreign keys

Original address: http://lpyyn.iteye.com/blog/2180551      

        Regarding how to disable Hibernate from generating foreign keys, there is a way to set ForeignKey(name="null") on the Internet, so that Hibernate does not generate foreign key associations, but it needs to be set on each association relationship, which is cumbersome and difficult to uniformly control and ensure the database. There is no foreign key association. Moreover, it has been tested that setting foreignkey=@ ForeignKey(name="null") in @JoinColumn will not generate foreign keys, but this setting method can still generate foreign keys in @JoinTable. 

       The following provides a way to disable Hibernate foreign keys, and does not generate foreign key associations when creating database tables, but I personally feel that it is not the best solution, and I hope to give more advice.

       Idea: Because Hibernate defines a dialect for each database in order to deal with the differences in SQL of different databases, the corresponding SQL will be obtained from the method of the dialect class when executing SQL, so the method of generating foreign key SQL in the dialect class can be rewritten. Database foreign key associations are not generated. The Dialect classes of the Postgresql database and Oracle database rewritten respectively are as follows:

Java code   Favorite code
  1. import org.hibernate.dialect.PostgreSQL9Dialect;  
  2. /** 
  3.  * Do not generate foreign keys, modify the SQL that creates foreign keys for each database through a method similar to SQL injection 
  4.  */  
  5. public class PostgreSQL9DialectWithoutFK extends PostgreSQL9Dialect {  
  6.     @Override  
  7.     public String getAddForeignKeyConstraintString(  
  8.             String constraintName,  
  9.             String[] foreignKey,  
  10.             String referencedTable,  
  11.             String[] primaryKey,  
  12.             boolean referencesPrimaryKey) {  
  13. // Set the column value corresponding to foreignkey to be empty  
  14.         return " alter "+ foreignKey[0] +" set default null " ;  
  15.     }  
  16. }  
Java code   Favorite code
  1. import org.hibernate.dialect.Oracle10gDialect;  
  2. /** 
  3.  * Do not generate foreign keys, modify the SQL that creates foreign keys for each database through a method similar to SQL injection 
  4.  */  
  5. public class Oracle10gDialectWithoutFK extends Oracle10gDialect {  
  6.     @Override  
  7.     public String getAddForeignKeyConstraintString(  
  8.             String constraintName,  
  9.             String[] foreignKey,  
  10.             String referencedTable,  
  11.             String[] primaryKey,  
  12.             boolean referencesPrimaryKey) {  
  13. // Avoid generating foreign keys by modifying the default value of the foreign key column instead of adding a foreign key  
  14.         return " modify "+ foreignKey[0] +" default null " ;  
  15.     }  
  16. }  

       When rewriting the generated foreign key SQL, I considered using SQL injection to delete the foreign key after creating the foreign key, but this method is more complicated, and I gave up after doing a little.

 

       Created an overridden Dialect class, configured with hibernate.dialect= Oracle10gDialectWithoutFK, the foreign key strategy will take effect when the database table is generated.

      Other:

  • The discussion on whether to generate foreign keys in the database is as shown in the figure below. The specific discussion content can be searched for related content by keyword.


  • Configuration of foreign keys not generated by @JoinColumn in JPA
  • The alter syntax in postgresql is as follows

     

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326645555&siteId=291194637