Trying to write Dao Test cases for Dropwizard application.
I have implemented my own version to AbstractDaoTest class which has nothing todo with Dropwizard's configuration. Wondering if I can use Dropwizard's hibernate configuration and get session factory from Dropwizard's HibernateBundle.
public AbstractDaoTest() {
Configuration config=new Configuration();
config.setProperty("hibernate.connection.url","jdbc:mysql://127.0.0.1/testme");
config.setProperty("hibernate.connection.username","haha");
config.setProperty("hibernate.connection.password","haha");
config.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
config.setProperty("hibernate.current_session_context_class","thread");
config.setProperty("hibernate.show_sql", "true");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
sessionFactory = config.buildSessionFactory(serviceRegistry);
}
Is there a way I can get hold of HibernateBundle of Dropwizard in my AbstarctDaoTest class?
Related
Hibernate 5.4
The dialect is known, I need to implement the following method :
#Bean
public org.hibernate.SessionFactory sessionFactory(DataSource dataSource) {
// hibernate.dialect = "org.hibernate.dialect.PostgreSQL95Dialect"
// ???
}
Maybe creating one via a Configuration would work for you, as descibed here:
Create Sessionfactory in Hibernate
Configuration cfg = new Configuration()...
.setProperty("hibernate.connection.username", "myuser");
.setProperty("hibernate.connection.password", "mypassword")
.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate_example")
SessionFactory sessionFactory = cfg.buildSessionFactory();
Or you choose to use the Spring LocalSessionFactory-Class like so:
lsfb = new LocalSessionFactoryBean() [from hib5-package]
lsfb.setDataSource( yourDS );
return lsfb.getObject();
Setting the Datasource this might help:
Luiggi Mendoza on SO (How can I set Datasource when I'm creating Hibernate SessionFactory?
)
But if you use a custom data source provider like Apache DBCP or BoneCP and you don't want to use a dependency injection framework like Spring, then you may inject it on the StandardServiceRegistryBuilder before creating the SessionFactory...
I want to a configure JPA in a java class (not using the usual persistence.xml approach)
Here is an example of what I'm doing:
public SessionFactory getSessionFactory(){
Configuration configuration=new Configuration();
Properties settings= new Properties();
settings.put(Environment.DRIVER, "");
settings.put(Environment.URL, "");
settings.put(Environment.USER, "");
settings.put(Environment.PASS, "");
settings.put(Environment.DIALECT, "");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.DEFAULT_SCHEMA, "");
configuration.setProperties(settings);
configuration.addAnnotatedClass(Passenger.class);
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory=configuration.buildSessionFactory(serviceRegistry);
}
If you cannot (or don't want) use the standard XML configuration, you can bootstrap JPA via a custom implementation. Here is an example.
If you provide more info about your desired result (dialect, persistence unit name etc.) I can give you a more detailed example.
I just got through the book "Just Hibernate" from O'Reilly. Some code isn't really explained fully but just given without complete description.
This code for example:
public class BasicMovieManager {
private SessionFactory sessionFactory = null;
// Creating SessionFactory using 4.2 version of Hibernate
private void initSessionFactory(){
Configuration config = new Configuration().configure();
// Build a Registry with our configuration properties
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
// create the session factory
sessionFactory = config.buildSessionFactory(serviceRegistry);
}
...
}
I just wanted to copy the code to my Hibernate-experiments, but the current stable Hibernate-version 5.2 doesn't know the class ServiceRegistryBuilder. What is a service-registry and how do I have to change the code to work with the current Hibernate-version?
The code was used to create a SessionFactory with Hibernate 4.x
The similar code for Hibernate 5.x would be something like:
StandardServiceRegistry standardRegistry =
new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata metaData =
new MetadataSources(standardRegistry).getMetadataBuilder().build();
sessionFactory = metaData.getSessionFactoryBuilder().build();
As you can see, in Hibernate 5 StandardServiceRegistry class is used. If you don't have a hibernate.cfg.xml file just use configure() method with no arguments.
See this article for further details.
In the SDN 4.1 reference, the configuration includes extending Neo4jConfiguration and setting up a Session object explicitly as a #Bean. In 4.2, the guidance is to not extend Neo4jConfiguration and follow the config below. Note that explicitly setting up a standalone Session object is absent:
#Bean
public org.neo4j.ogm.config.Configuration configuration() {
org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
config
.driverConfiguration()
.setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver")
.setURI(dbLocation);
return config;
#Bean
public SessionFactory sessionFactory() {
SessionFactory sessionFactory = new SessionFactory(configuration(), "org.my.package" );
sessionFactory.register(new MyPreSaveListener());
return sessionFactory;
}
I've seen this config used while #Autowiring the Session object itself (not the factory) in repository classes. Does this mean that there will then be only one Session instance across the application? If so, doesn't that go against the idea that a Session lifespan should be limited to an application's "unit of work?"
Note that my repositories are custom and do not extend the neo4j repos, as I am currently migrating away from using the Neo4jTemplate object.
No, there is not one session per application. So long as your Session is called from within a #Transactional annotation or TransactionTemplate it will call a proxy Session (which is generated by the Spring Data Neo4j framework during startup). This will effectively create a session for the lifetime of the transaction (where it is demarcated) and once out of scope, allow it to be garbage collected.
I am using a hibernate java application which uses a sessionfactory to create connections and sessions. As i am using it with a postgresql database i pass the proper jdbc connectionstring to build the sessionfactory and get my sessions from it afterwards. The only thing im able to access is the jdbc4connection.
How am i able to read which ciphersuite is used within the secured connection, which SSL protocol is used etc?
Here is how i initialize my sessionfactory:
Configuration configuration = new Configuration();
Properties p = configuration.getProperties();
p.setProperty("hibernate.connection.url","jdbc:postgresql://127.0.0.1:5432/postgres?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory");
p.setProperty("hibernate.connection.username", "myusername");
p.setProperty("hibernate.connection.password", "mypassword");
p.setProperty("hibernate.connection.driver_class",
"org.postgresql.Driver");
p.setProperty("hibernate.dialect",
"org.hibernate.dialect.PostgreSQLDialect");
ServiceRegistry serviceRegistryWebOnkys = new StandardServiceRegistryBuilder()
.applySettings(p).build();
SessionFactory sessionFactory = configuration
.buildSessionFactory(serviceRegistryWebOnkys);
If you can find out how to do it with via regular JDBC (I don't know how it's done), then you can do the same thing with Hibernate.
session.doWork(new Work() {
#Override
public void execute(Connection connection) throws SQLException {
// access the connection here and perform regular jdbc operations.
}
});
This is what I had to do when I had set some Oracle specific properties that could be set only on "OracleConnection.java" instances.
oracle.jdbc.driver.OracleConnection oc = (oracle.jdbc.driver.OracleConnection) connection.getMetaData().getConnection()