Java 使用hibernate在运行时为derby数据库设置引导密码
我使用Hibernate和Derby 我有一个hibernate.cfg.xml以及我为使用db waas获得会话所做的一切:Java 使用hibernate在运行时为derby数据库设置引导密码,java,xml,hibernate,derby,Java,Xml,Hibernate,Derby,我使用Hibernate和Derby 我有一个hibernate.cfg.xml以及我为使用db waas获得会话所做的一切: return new AnnotationConfiguration().configure( "files/hibernate.cfg.xml" ).buildSessionFactory().getCurrentSession(); 我的hibernate.cfg.xml包含 <property name="connection.driver_
return new AnnotationConfiguration().configure( "files/hibernate.cfg.xml" ).buildSessionFactory().getCurrentSession();
我的hibernate.cfg.xml包含
<property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="connection.url">jdbc:derby:crmdb;create=true</property>
如果我只删除bootPassword,它就无法连接到db
有什么想法吗?在构建sessionFactory之前,您需要修改配置:
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory(){
if(sessionFactory==null){
Configuration configuration = new AnnotationConfiguration().configure( "files/hibernate.cfg.xml" );
configuration.setProperty("hibernate.connection.url", "jdbc:derby:crmdb;create=true;dataEncryption=true;bootPassword="+password);
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
}
return sessionFactory;
}
其他备注:您必须避免每次都重新创建一个新的SessionFactory(这需要花费大量时间,消耗大量资源,而且毫无用处)。i、 e.您必须为每个bootPassword创建一个sessionFactory(因为它是唯一的动态部分),如果您只有一个bootPassword(即一个DB),那么您的sessionFactory可以/必须是单例
问题解决了
我应该设置“hibernate.connection.url”而不是“connection.url” @ref非常感谢您对sessionFactory的回答和提示。我更新了sessionFactory的代码。但是连接问题已经存在。在读取cfg.xml文件后,配置似乎不接受setProperty。抱歉。。。我的代码中有错误:1。属性的前缀必须为
hibernate.
2。更新属性后需要调用configure()
。(帖子更新)谢谢。我不认为再次调用configure()可以。因为它会再次覆盖所有设置。
java.lang.UnsupportedOperationException: The application must supply JDBC connections
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory(){
if(sessionFactory==null){
Configuration configuration = new AnnotationConfiguration().configure( "files/hibernate.cfg.xml" );
configuration.setProperty("hibernate.connection.url", "jdbc:derby:crmdb;create=true;dataEncryption=true;bootPassword="+password);
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
}
return sessionFactory;
}