Skip to content

Instantly share code, notes, and snippets.

@rschumm
Created July 13, 2016 12:16
Show Gist options
  • Save rschumm/04c7834f47cf76761814e05f47dc51aa to your computer and use it in GitHub Desktop.
Save rschumm/04c7834f47cf76761814e05f47dc51aa to your computer and use it in GitHub Desktop.
JPA Standalone
package ch.schumm.orgdaten.tools;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import ch.schumm.orgdaten.model.Employee;
import com.google.common.io.ByteStreams;
/**
*/
public class ImageImporterJPA {
EntityManager em = null;
public ImageImporterJPA(EntityManager em) {
this.em = em;
}
public static void main(String[] args) throws SQLException, IOException {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "orgdatenapi-direct-PU" );
// writeToDb();
ImageImporterJPA imageImporterJPA = new ImageImporterJPA(entityManagerFactory.createEntityManager());
imageImporterJPA.writeAvatars();
System.exit(0);
}
private void writeAvatars() throws IOException, FileNotFoundException {
String base = "C:\\Users\\Desktop\\remy_csv\\bilder";
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(base));
em.getTransaction().begin();
for (Path path : directoryStream) {
System.out.println(path);
byte[] bild = ByteStreams.toByteArray(new FileInputStream(path.toFile()));
String[] split = path.getFileName().toString().split("\\.");
String nameEmail = split[0] + "." +split[1];
List<Employee> emplsWithThatMail = searchEmployeeByNameEmail(nameEmail);
for (Employee employee : emplsWithThatMail) {
System.out.println("...gesetzt für :" + employee.getLastname());
employee.setAvatar(bild);
em.persist(employee);
}
}
em.flush();
em.getTransaction().commit();
}
private List<Employee> searchEmployeeByNameEmail(String nameEmail) {
String email = nameEmail + "@axa-winterthur.ch";
TypedQuery<Employee> findAvatarByMail = em.createQuery("select a from Employee a where a.email = :email",
Employee.class);
findAvatarByMail.setParameter("email", email);
return findAvatarByMail.getResultList();
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- in /src/main/resources/META-INF/persistence.xml -->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="orgdatenapi-direct-PU"
transaction-type="RESOURCE_LOCAL">
<description>Persistence Unit für Kommandozeilentools ohne JBoss</description>
<class>ch.schumm.orgdaten.model.Employee</class>
<class>ch.schumm.orgdaten.model.Agency</class>
<class>ch.schumm.orgdaten.model.Employment</class>
<class>ch.schumm.orgdaten.model.EmploymentID</class>
<class>ch.schumm.orgdaten.model.imported.EmployeeReviewed</class>
<class>ch.schumm.orgdaten.model.imported.EmployeeImport</class>
<class>ch.schumm.orgdaten.model.Language</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<property name="javax.persistence.jdbc.driver"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:sqlserver://asdf:1433;databaseName=dbname" />
<property name="javax.persistence.jdbc.user" value="user" />
<property name="javax.persistence.jdbc.password" value="öalkfwofaöldkfjasldjwoekfadlfjkasd" />
</properties>
</persistence-unit>
</persistence>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ch.schumm.orgdaten</groupId>
<artifactId>orgdatenapi</artifactId>
<version>1.0.10-SNAPSHOT</version>
</parent>
<artifactId>tools</artifactId>
<name>Tools </name>
<description>Tools für OrgDatAPI, wie Datenimport etc. </description>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- DB Provisionning - Liquibase -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
</dependency>
<dependency>
<groupId>ch.schumm.orgdaten</groupId>
<artifactId>model</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment