我如何用MySQL数据库中的EntityManager编写?

时间:2015-06-30 22:14:44

标签: java mysql java-ee dao entitymanager

我有一个java ee应用程序并使用JPA。 (EJB)。我有两个实体。

故障单和用户类。

我的问题是我不能在数据库中写用户:(门票有效......!

这是用户类:

@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{

...

}

这是UserDAO界面:

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);
}

这是实现UserDAO的UserBean:

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

    @PersistenceContext
    private EntityManager em;

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

}

这是创建用户和故障单的类:

@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);
        }

    }
}

如果我启动应用程序,我可以在MySQL数据库中看到故障单是创建的但不是用户:(

控制台中的错误:

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

0 个答案:

没有答案