Hi Friends,
I'm trying a sample app in which i'm using EJB3 ,JPA and Hibernate.
In this application i'm trying to persist a pojo(entity bean) into database and i'm able to do that.
But the problem is when i undeploy or redeploy the application the data in Database is getting deleted don't know how!!
Here's complete code:
persistence.xml
------------------
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
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" version="1.0">
<persistence-unit name="EJB3P1PU" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>java:/MySqlDS</non-jta-data-source>
<class>com.kalki.entity.Customer</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
-------------------------------------------
Hello.java--->StatelessSessionBean
@Remote(HelloRemote.class)
@Stateless
@TransactionManagement(value=TransactionManagementType.BEAN)
public class Hello implements Serializable, HelloRemote {
@PersistenceUnit(unitName="EJB3P1PU")
private EntityManagerFactory factory;
//EntityManager em;
EntityTransaction et;
Query q;
@Resource
private EJBContext context;
protected Customer cust;
public void insert(String name,int phone)
{
EntityManager entityManager = factory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
System.out.println("--------inside hello bean------");
cust=new Customer(name,phone);
entityManager.persist(cust);
entityTransaction.commit();
entityManager.close();
}
}
----------------------------------------------------------
Customer.java---->Entity Bean
@Entity
@Table(name="customer")
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private int phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public Customer() {
}
public Customer(String name, int phone)
{
this.name=name;
this.phone=phone;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public int hashCode() {
return (this.id == null) ? 0 : this.id.hashCode();
}
public boolean equals(Object object) {
if (object instanceof Customer) {
final Customer obj = (Customer) object;
return (this.id != null) ? this.id.equals(obj.id)
: (obj.id == null);
}
return false;
}
}
-------------------------------------
HelloRemote.java ---->Remote Interface
import javax.ejb.Remote;
@Remote
public interface HelloRemote {
public void insert(String name,int phone);
}
------------------------------------
Client.java ----> Client class
public class Client {
public static void main(String[] args) {
try {
InitialContext ctx = new InitialContext();
HelloRemote bean=(HelloRemote)ctx.lookup("Hello/remote");
bean.insert("anil",996985);
System.out.println("inserted-------------");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
--------------------------------------
Help me out if i'm wrong or i missed to configure something which is required.
Thanks in advance,
Anil.