gpt4 book ai didi

java - 有没有办法从 Hibernate 使用 Postgresql 副本(在表中加载 CSV)?

转载 作者:行者123 更新时间:2023-11-29 12:04:24 29 4
gpt4 key购买 nike

在我当前的应用程序中,我使用的是 Hibernate + PostgreSQL。对于特定情况,我需要使用 postgres 中可用的 COPY 功能从 CSV 文件加载数据。有什么方法可以使用 Hibernate 来使用 COPY

Postgres version : 9.4
Hibernate version : 5.0.6

最佳答案

基于@a-horse-with-no-name 评论,我将以下代码放在一起,因为 session.doWork 已被弃用。

SessionFactory factory = HibernateUtil.getSessionFactory();
Session session = factory.openSession();
SessionImplementor sessImpl = (SessionImplementor) session;
Connection conn = null;
conn = sessImpl.getJdbcConnectionAccess().obtainConnection();
CopyManager copyManager = new CopyManager((BaseConnection) conn);
File tf =File.createTempFile("temp-file", "tmp");
String tempPath =tf.getParent();
File tempFile = new File(tempPath + File.separator + filename);
FileReader fileReader = new FileReader(tempFile);
copyManager.copyIn("copy testdata (col1, col2, col3) from STDIN with csv", fileReader );

希望对读者有所帮助。

关于java - 有没有办法从 Hibernate 使用 Postgresql 副本(在表中加载 CSV)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35057346/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com