mardi 30 juin 2015

How I write with the EntityManager in MySQL Database?

hi i have a java ee application and use JPA. (EJB). I have two Entities.

The Ticket and the User Class.

My Problem is that I dont can write the User in the Database :( The Ticket works...!

here is the user class:

@Entity
@NamedQueries({
    @NamedQuery(name=User.QUERY_GETALL,query="SELECT c FROM User c"),
    @NamedQuery(name=User.QUERY_GETALLTICKETS,query="SELECT c FROM Ticket c WHERE c.username LIKE :userUsername"),
})
public class User implements Serializable{

...

}

here is the UserDAO Interface:

public interface UserDAO {

    public User create(User user);

    public User update(User user);

    public void remove(int id);

    public User getUser(int id);

    public List<User> getAllUsers();

    public List<Ticket> getAllUserTickets(String username);
}

here is the UserBean that implements the UserDAO:

@Stateless
@Remote(UserDAO.class)
public class UserBean implements UserDAO{

    @PersistenceContext
    private EntityManager em;

    @Override
    public User create(User user) {
        em.persist(user);   
        return user;
    }
...

}

here is the class for creating the User and the Ticket:

@Singleton
@Startup
public class InitializationBean {

    @EJB
    private TicketDAO ticketDAO;

    @EJB
    private UserDAO userDAO;

    @PostConstruct
    private void initialize(){

        if(ticketDAO.getAllTickets().size() == 0){

            //User Dummy anlegen

            User user = new User();

            user.setFirstname("Wladimir");
            ...

            userDAO.create(user);

            //Ticket Dummy anlegen

            Ticket ticket = new Ticket();

            ticket.setName("DummyTicket");
            ...

            ticketDAO.create(ticket);
        }

    }
}

If I start the Application I can see in the MySQL Database that the Ticket was create but not the User :(

The Errors in the console:

00:07:52,486 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (ServerService Thread Pool -- 59) Data truncation: Data too long for column 'user' at row 1

00:07:52,579 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 59) MSC000001: Failed to start service jboss.deployment.subunit."TaraTicket.ear"."TaraTicketEJB.jar".component.InitializationBean.START: org.jboss.msc.service.StartException in service jboss.deployment.subunit."TaraTicket.ear"."TaraTicketEJB.jar".component.InitializationBean.START: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance

Aucun commentaire:

Enregistrer un commentaire