Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save juarezjuniorgithub/3ac7fede03bad68c96bca2ffafaa5d94 to your computer and use it in GitHub Desktop.
Save juarezjuniorgithub/3ac7fede03bad68c96bca2ffafaa5d94 to your computer and use it in GitHub Desktop.
HibernateVectorDataTypeOracleDatabase.java
/*
Copyright (c) 2024, Oracle and/or its affiliates.
This software is dual-licensed to you under the Universal Permissive License
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
either license.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.oracle.dev.jdbc;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.Properties;
import java.util.stream.IntStream;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateVectorDataTypeOracleDatabase {
private Configuration configuration;
private SessionFactory sessionFactory;
public static void main(String[] args) throws SQLException {
HibernateVectorDataTypeOracleDatabase example = new HibernateVectorDataTypeOracleDatabase();
// insert vectors
IntStream.rangeClosed(1, 5).forEach(i -> example.insertVector());
// read a vector
HibernateVectorDataTypeEntity entity = example.readVector(1L);
if (entity != null) {
System.out.println("Embedding retrieved: " + entity.getEmbedding()[0] + ", " + entity.getEmbedding()[1] + ", "
+ entity.getEmbedding()[2]);
// update a vector
entity.setEmbedding(new float[] { 4.0f, 5.0f, 6.0f });
example.updateVector(entity);
// delete a vector
example.deleteVector(1L);
}
}
public HibernateVectorDataTypeOracleDatabase() {
Properties properties = loadProperties();
configuration = new Configuration().addProperties(properties)
.addAnnotatedClass(HibernateVectorDataTypeEntity.class);
sessionFactory = configuration.buildSessionFactory();
}
private void insertVector() {
// Open a session
try (Session session = sessionFactory.openSession()) {
// Begin a transaction
Transaction transaction = session.beginTransaction();
// Create and save a HibernateVectorDataTypeEntity instance
HibernateVectorDataTypeEntity entity = new HibernateVectorDataTypeEntity();
// Set the embedding
// float[] embeddingArray = new float[] { 1.0f, 2.0f, 3.0f };
// VECTOR vectorValue = VECTOR.ofFloat32Values(embeddingArray);
float[] embeddingArray = new float[] { 1.0f, 2.0f, 3.0f };
entity.setEmbedding(embeddingArray);
// Save the entity
session.persist(entity);
// Commit the transaction
transaction.commit();
}
}
public HibernateVectorDataTypeEntity readVector(long id) {
try (Session session = sessionFactory.openSession()) {
return session.get(HibernateVectorDataTypeEntity.class, id);
}
}
public void updateVector(HibernateVectorDataTypeEntity entity) {
try (Session session = sessionFactory.openSession()) {
Transaction transaction = session.beginTransaction();
session.merge(entity);
transaction.commit();
}
}
public void deleteVector(long id) {
try (Session session = sessionFactory.openSession()) {
Transaction transaction = session.beginTransaction();
HibernateVectorDataTypeEntity entity = session.get(HibernateVectorDataTypeEntity.class, id);
if (entity != null) {
session.remove(entity);
}
transaction.commit();
} finally {
sessionFactory.close();
}
sessionFactory = null;
configuration = null;
}
private Properties loadProperties() {
Properties properties = new Properties();
try (FileInputStream inputStream = new FileInputStream(
Paths.get("src/main/resources/hibernate.properties").toFile())) {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment