[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Q : getting started using EclipseLink ?

i tried this :

Main.java
...

public class Main {
    public static void main(String[] args){
         EntityManagerFactory emf =
                           Persistence.createEntityManagerFactory("HelloJPA");
         EntityManager em = emf.createEntityManager();
         UserService service = new UserService(em);
           
         em.getTransaction().begin();
         User emp = service.createEmployee("aaa", "123", "admin");
         em.getTransaction().commit();
    }
}

=========================================================

User.java

...

@Entity
@Table(name="USER", schema="database")
public class User implements Serializable {

      
    @Id
    private String Username;
    private String Password;
    private String Status;
    private static final long serialVersionUID = 1L;

    public User() {
        super();
    }  
    public String getUsername() {
        return this.Username;
    }

    public void setUsername(String Username) {
        this.Username = Username;
    }  
    public String getPassword() {
        return this.Password;
    }

    public void setPassword(String Password) {
        this.Password = Password;
    }  
    public String getStatus() {
        return this.Status;
    }

    public void setStatus(String Status) {
        this.Status = Status;
    }
  
}
=================================================
UserService.java

....

public class UserService {
    protected EntityManager em;
    public UserService(EntityManager em) {
        this.em = em;
    }

    public User createEmployee(String username, String password, String status) {
        User emp = new User();
        emp.setUsername(username);
        emp.setPassword(password);
        emp.setStatus(status);
        em.persist(emp);
        return emp;
    }

    public void removeUser(String username) {
        User emp = findUser(username);
        if (emp != null) {
            em.remove(emp);
        }
    }

    public User findUser(String username) {
        return em.find(User.class, username);
    }

    public Collection<User> findAllUsers() {
        Query query = em.createQuery("SELECT e FROM User e");
        return (Collection<User>) query.getResultList();
    }
}

==========================================================

Persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
    <persistence-unit name="HelloJPA" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>
        edu.its.bean.User</class>
        <properties>
            <property name="eclipselink.jdbc.driver" value="org.gjt.mm.mysql.Driver"/>
            <property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/database" />
            <property name="eclipselink.jdbc.user" value="root" />
            <property name="eclipselink.jdbc.password" value="" />
            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
            <property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
            <property name="eclipselink.jdbc.bind-parameters" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

=================================================

and i got an error :
Exception in thread "main" java.lang.NullPointerException
    at java.util.Hashtable.put(Unknown Source)
    at java.util.Properties.setProperty(Unknown Source)
    at com.mysql.jdbc.NonRegisteringDriver.parseURL(NonRegisteringDriver.java:756)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:277)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:97)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:164)
    at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connectInternal(DatasourceAccessor.java:324)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.connectInternal(DatabaseAccessor.java:264)
    at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connect(DatasourceAccessor.java:407)
    at org.eclipse.persistence.sessions.server.ConnectionPool.buildConnection(ConnectionPool.java:130)
    at org.eclipse.persistence.sessions.server.ConnectionPool.startUp(ConnectionPool.java:367)
    at org.eclipse.persistence.sessions.server.ServerSession.connect(ServerSession.java:500)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:606)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:211)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:246)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:69)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:118)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:112)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:100)


is there something wrong with my code ??


thnx u


dwi ardi irawan - 'penyihirkecil'
http://dwiardiirawan.blogspot.com


Dapatkan alamat Email baru Anda!
Dapatkan nama yang selalu Anda inginkan sebelum diambil orang lain!