Hibernate将sql保存到文件中

时间:2014-11-07 09:17:31

标签: java sql hibernate file

我想将实体序列化为SQL查询字符串并将其保存到.txt文件中,以便以后能够将其写入数据库。无论SQL方言如何,我都需要解决方案才能工作,所以我想使用Hibernate。

Hibernate可以做到吗?你能给我一个线索或教程,我怎么能做到这一点?

我们假设我们有一个实体:

@Entity  
public class Project {
       @Id
       @GeneratedValue(strategy = GenerationType.SEQUENCE)
       private Long id;
       private String name;

       //... setters, getters and constructor
}

我想将一些实体序列化为SQL语句字符串,如下所示:

INSERT INTO project (name)
VALUES ('SQL Convertion Project');
INSERT INTO project (name)
VALUES ('DB Migration');
INSERT INTO project (name)
VALUES ('Stackoverflow is cool');

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。我希望它会对某人有所帮助。

下面的代码使用Hibernate实体persister获取实体Project的插入和更新查询。如果需要,我可以注入查询参数并将查询写入文件。

  1. 仅使用Hibernate框架:
  2. Configuration configuration = new Configuration().configure();
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
            applySettings(configuration.getProperties());
    SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
    
    ClassMetadata hibernateMetadata = sessionFactory.getClassMetadata("com.mypackage.Project ");
    if (hibernateMetadata == null){
        return;
    }
    
    if (hibernateMetadata instanceof AbstractEntityPersister) {
        AbstractEntityPersister persister = (AbstractEntityPersister) hibernateMetadata;
    
        for (Method method : persister.getClass().getSuperclass().getDeclaredMethods()) {
            if (method.getName().equalsIgnoreCase("getSQLInsertStrings")) {
                ReflectionUtils.makeAccessible(method);
                Object insertQueries = ReflectionUtils.invokeMethod(method, persister);
    
                for (String queryText : (String []) insertQueries) {
                    System.out.println(queryText);
                }
            }
    
            if (method.getName().equalsIgnoreCase("getSQLUpdateStrings")) {
                ReflectionUtils.makeAccessible(method);
                Object insertQueries = ReflectionUtils.invokeMethod(method, persister);
    
                for (String queryText : (String []) insertQueries) {
                    System.out.println(queryText);
                }
            }
        }
    }
    
    1. 使用Hibernate + Spring
    2.   

      ApplicationContext ctx = new ClassPathXmlApplicationContext(               “applicationContext.xml中”);

      EntityManager em = ((EntityManagerFactory) ctx.getBean("entityManagerFactory")).createEntityManager();
      try {
          Session session = em.unwrap(Session.class);
      
          ClassMetadata hibernateMetadata = session.getSessionFactory().getClassMetadata("com.mypackage.Project");
          if (hibernateMetadata == null){
              return;
          }
          if (hibernateMetadata instanceof AbstractEntityPersister) {
              AbstractEntityPersister persister = (AbstractEntityPersister) hibernateMetadata;
      
              for (Method method : persister.getClass().getSuperclass().getDeclaredMethods()) {
                  if (method.getName().equalsIgnoreCase("getSQLInsertStrings")) {
                      ReflectionUtils.makeAccessible(method);
                      Object insertQueries = ReflectionUtils.invokeMethod(method, persister);
      
                      for (String queryText : (String []) insertQueries) {
                          System.out.println(queryText);
                      }
                  }
      
                  if (method.getName().equalsIgnoreCase("getSQLUpdateStrings")) {
                      ReflectionUtils.makeAccessible(method);
                      Object insertQueries = ReflectionUtils.invokeMethod(method, persister);
      
                      for (String queryText : (String []) insertQueries) {
                          System.out.println(queryText);
                      }
                  }
              }
      
          }
      }
      finally {
          if (em != null) {
              em.close();
          }
      }