Meaningless Drivel is fun!*
The moose likes EJB and other Java EE Technologies and the fly likes Problem -EJB3+JPA+HIbernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Problem -EJB3+JPA+HIbernate" Watch "Problem -EJB3+JPA+HIbernate" New topic
Author

Problem -EJB3+JPA+HIbernate

anil gowda
Greenhorn

Joined: Jun 12, 2007
Posts: 12
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.

Chandrashekhar Kotekar
Greenhorn

Joined: Aug 17, 2009
Posts: 14
@Anil,



this property in properties.xml file is the root of your problem.

Also, just wanted to know is your entire database is getting deleted? or only table or rows?
anil gowda
Greenhorn

Joined: Jun 12, 2007
Posts: 12

Only rows are getting deleted whenever i deploy or redeploy the application.

Does i need to change my persistence.xml file. if so what should i add more ?? can you please help me out.

Thank you.
Chandrashekhar Kotekar
Greenhorn

Joined: Aug 17, 2009
Posts: 14
Im sorry but I do not know much about JPA. What I think is the line I mentioned in my reply is causing the table to get truncated as soon as you reload your classes on database.
Stu Quinn
Ranch Hand

Joined: Feb 27, 2009
Posts: 35
To create the schema upon deployment use create. If you want the schema to be deleted when undeployed use create-drop. If you do not want to autogen the schema at all leave this property off.

See below.

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"/>
</properties>
</persistence-unit>

</persistence>


Good luck
anil gowda
Greenhorn

Joined: Jun 12, 2007
Posts: 12
Thanks a lot
Sib Pete
Greenhorn

Joined: Apr 27, 2009
Posts: 26
I believe that you can also use "update" as an option. That way if there are any schema changes, it can be applied on redeployed instead of recreating from scratch.
 
 
subject: Problem -EJB3+JPA+HIbernate