This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Can't persist data into Oracle 10g by the default persistence layer of EJB 3.0 (Hibernate) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Can Watch "Can New topic
Author

Can't persist data into Oracle 10g by the default persistence layer of EJB 3.0 (Hibernate)

tan dung dang
Greenhorn

Joined: Oct 09, 2008
Posts: 6
Hello pro,
I am trying to insert data into Oracle 10g but it is not successful. I had NO WARNING, NO ERRORS, but I STILL CAN'T persist an object :pissed: .

- User.java file
@Entity
public class User implements Serializable {
private String username;
private String password;
private Long id;

public User() {
super();
}

public User(String username, String password) {
this.username = username;
this.password = password;
}

public String getUsername() {
return username;
}
public String getPassword() {
return password;
}

public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}

@Id
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}

- persistence.xml file:
<persistence-unit name="scbcdejb">
<mapping-file>META-INF/orm.xml</mapping-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
<property name="hibernate.connection.username" value="dzungdev" />
<property name="hibernate.connection.password" value="dzungdev" />
<property name="hibernate.connection.url" value="jdbc:oracle:thin:dzungdev/dzungdev@127.0.0.1:1521:xe" />
<property name="hibernate.show_sql" value="true"/>
<!--<property name="hibernate.max_fetch_depth" value="3" />
--></properties>

</persistence-unit>

- orm.xml file:
<entity class="dzung.entity.User" access="PROPERTY">
<table name="M3_USER"></table>
<sequence-generator name="user_seq" sequence-name="M3_USER_SEQ"/>
<attributes>
<id name="id">
<column name="id" />
<generated-value strategy="SEQUENCE" generator="user_seq"/>
</id>
<basic name="password">
<column name="password" />
</basic>
<basic name="username">
<column name="username" />
</basic>
</attributes>
</entity>

- This is Client.java file:

public class Client {
public static void main(String[] args) {
EntityManagerFactory
emf = Persistence.createEntityManagerFactory("scbcdejb");
EntityManager em = emf.createEntityManager();
try {
User u1 = new User("dzungdev", "test");
em.persist(u1);
System.out.println("Go here");
} finally {
em.close();
emf.close();
}
}
}

My console is display with: Hibernate: select M3_USER_SEQ.nextval from dual
Go here

NO ERROR, NO WARNING but when I go to the schema to see data, nothing in the M3_USER table.

Are there someone to give me an advice :(
Uppala Ramana
Greenhorn

Joined: Jan 14, 2010
Posts: 14
Tan,

You have to use transactions...to sync your persistent entities to databse.

try{
entityManager.getTransaction().begin();
//persist
//merge
entityManager.getTransaction().commit();
}catch(Exception exception){
entityManager.getTransaction().rollback();
}

Thanks,
Ramana.


Ramana Uppala
SCJA,SCJP
tan dung dang
Greenhorn

Joined: Oct 09, 2008
Posts: 6
Thank Uppala, now it runs well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't persist data into Oracle 10g by the default persistence layer of EJB 3.0 (Hibernate)
 
It's not a secret anymore!