Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :(
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
tan dung dang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Uppala, now it runs well.
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic