Hi,
I would like Drools to save my POJO objects as Hibernate Entities so that I can see my POJO objects as db tables in my database.
These are my relevant files:
1.
ApplicationContext.xml
<?xml
version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:drools="http://drools.org/schema/drools-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://drools.org/schema/drools-spring
drools-spring.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean
id="dataSourceH2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property
name="driverClassName"
value="org.h2.Driver"
/>
<property
name="url"
value="jdbc:h2:tcp://localhost/~/hibernateExample"
/>
<property
name="username"
value="sa"
/>
<property
name="password"
value=""
/>
</bean>
<bean
id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property
name="dataSource"
ref="dataSourceH2"
/>
<property
name="persistenceUnitName"
value="stewardcareH2"
/>
<!-- The persistenceXmlLocation needs to be set so that the application will
not incorrectly use the test version. See:
http://stackoverflow.com/a/2949496 -->
<property
name="persistenceXmlLocation"
value="META-INF/persistence.xml"
/>
</bean>
<bean
id="txManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property
name="entityManagerFactory"
ref="entityManagerFactory"
/>
</bean>
<drools:grid-node
id="CAREEngineNode"
/>
<drools:kstore
id="CAREKnowledgeStore"
/>
<drools:kbase
id="CAREEngineKBase"
node="CAREEngineNode">
<drools:configuration>
<drools:assert-behavior
mode="EQUALITY"
/>
</drools:configuration>
<drools:resources>
<drools:resource
type="DRL"
source="classpath:lily.drl"
/>
</drools:resources>
</drools:kbase>
</beans>
2. persistence.xml:
<?xml
version="1.0"
encoding="UTF-8"?>
<persistence
version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd">
<persistence-unit
name="stewardcareH2"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.drools.persistence.info.SessionInfo</class>
<class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
<class>org.drools.persistence.info.WorkItemInfo</class>
<class>com.mkyong.stock.Stock</class>
<class>com.mkyong.stock.StockDailyRecord</class>
<properties>
<property
name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect"
/>
<property
name="hibernate.max_fetch_depth"
value="3"
/>
<property
name="hibernate.hbm2ddl.auto"
value="create"
/>
<property
name="hibernate.show_sql"
value="true"
/>
<property
name="hibernate.connection.autocommit"
value="true"
/>
<!-- <property name="hibernate.transaction.manager_lookup_class" value= "org.hibernate.transaction.BTMTransactionManagerLookup"/> -->
<property
name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.BitronixJtaPlatform"/>
</properties>
</persistence-unit>
</persistence>
3. Rule file:
package com.mlyong.stock
//list any import classes here.
import com.mkyong.stock.Stock;
//declare any global variables here
rule
"My First Rule"
when
$stock: Stock(stockCode ==
"123")//conditions
then
System.out.println("lily");
//actions
end
4. Java code:
public
static
void main(String[] args)
throws Exception {
System.out.println("Hibernate
one to many (Annotation)");
// Session session = HibernateUtil.getSessionFactory().openSession();
// session.beginTransaction();
context =
new ClassPathXmlApplicationContext(APPLICATION_CONTEXT_XML);
KnowledgeStoreService kstore = (KnowledgeStoreService)
context.getBean("CAREKnowledgeStore");
Environment env = KnowledgeBaseFactory.newEnvironment();
env.set(EnvironmentName.ENTITY_MANAGER_FACTORY,
context.getBean("entityManagerFactory"));
BitronixTransactionManager transactionManager= TransactionManagerServices.getTransactionManager();
try {
UserTransaction ut = (UserTransaction)
new InitialContext().lookup("java:comp/UserTransaction");
ut.begin();
// transactionManager.begin();
env.set( EnvironmentName.TRANSACTION_MANAGER,
transactionManager);
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES,
new ObjectMarshallingStrategy[]{
new JPAPlaceholderResolverStrategy(env),
new SerializablePlaceholderResolverStrategy
(ClassObjectMarshallingStrategyAcceptor.DEFAULT)});
session = kstore.newStatefulKnowledgeSession((KnowledgeBase)context.getBean("CAREEngineKBase"),
null, env);
Stock stock =
new Stock();
stock.setStockCode("7052");
stock.setStockName("Lily");
session.insert(stock);
StockDailyRecord stockDailyRecords =
new StockDailyRecord();
stockDailyRecords.setPriceOpen(new
Float("1.2"));
stockDailyRecords.setPriceClose(new
Float("1.1"));
stockDailyRecords.setPriceChange(new
Float("10.0"));
stockDailyRecords.setVolume(3000000L);
stockDailyRecords.setDate(new
Date());
stockDailyRecords.setStock(stock);
stock.getStockDailyRecords().add(stockDailyRecords);
session.insert(stockDailyRecords);
ut.commit();
System.out.println("Done");
}
catch (SecurityException | IllegalStateException | RollbackException
| HeuristicMixedException | HeuristicRollbackException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
catch (SystemException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I can create tables in db, but there is data saved in the tables. Does anyone know what I am missing here?
Thank you so much in advance,
Hong Lily Ju
Software Engineer
Steward Heath Care
Office: 781-375-3034
Cell: 781-801-9313