这是我的JPA代码 我正在使用jpa,jsf,primefaces和maven。我也在使用mysql。实际上如果没有数据库,我可以将bean数据访问到我的jsf页面,但现在这些都没有用。我正在分享代码。
public class UsersJPA {
EntityManager em;
EntityManagerFactory emf;
private List<Users> users = new ArrayList<>();
public UsersJPA()
{
emf = Persistence.createEntityManagerFactory("com.kanbagisi_kanbagisi_war_1.0PU");
em = emf.createEntityManager();
em.getTransaction().begin();
}
public List<Users> getUsers()
{
try
{
users = em.createNamedQuery("Users.findAll",Users.class).getResultList();
return users;
}
catch (Exception e)
{
return users;
}
}}
这是我的ManagedBeans代码。其实我想要你使用CDI,但我是初学者。我也在使用netbeans IDE。
@ManagedBean
@SessionScoped
public class testBean implements Serializable {
private List<Users> userss = new ArrayList<>();
private UsersJPA ujpa = new UsersJPA();
public testBean() {
}
public List<Users> getUserss() {
return userss;
}
public void setUserss(List<Users> userss) {
this.userss = userss;
}
public UsersJPA getUjpa() {
return ujpa;
}
public void setUjpa(UsersJPA ujpa) {
this.ujpa = ujpa;
}
public List<Users> getir()
{
userss = ujpa.getUsers();
return userss;
}
}
Jsf代码(Primefaces)
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:dataTable value="#{testBean.getir()}" var="veri" >
<p:column headerText="SURNAME">
<h:outputText value="#{veri.surname}"/>
</p:column>
</p:dataTable>
</h:form>
</h:body>
答案 0 :(得分:-1)
这也是我的实体课程。但我认为这里没有错误,因为它是自动化的。
@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u")})
public class Users implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "user_id")
private Integer userId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "user_name")
private String userName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "user_password")
private String userPassword;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "name")
private String name;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "surname")
private String surname;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "mail_adress")
private String mailAdress;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "blood")
private String blood;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "tel_number")
private String telNumber;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "adress")
private String adress;
@Basic(optional = false)
@NotNull
@Column(name = "active_deactive")
private int activeDeactive;
public Users() {
}
public Users(Integer userId) {
this.userId = userId;
}
public Users(Integer userId, String userName, String userPassword, String name, String surname, String mailAdress, String blood, String telNumber, String adress, int activeDeactive) {
this.userId = userId;
this.userName = userName;
this.userPassword = userPassword;
this.name = name;
this.surname = surname;
this.mailAdress = mailAdress;
this.blood = blood;
this.telNumber = telNumber;
this.adress = adress;
this.activeDeactive = activeDeactive;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getMailAdress() {
return mailAdress;
}
public void setMailAdress(String mailAdress) {
this.mailAdress = mailAdress;
}
public String getBlood() {
return blood;
}
public void setBlood(String blood) {
this.blood = blood;
}
public String getTelNumber() {
return telNumber;
}
public void setTelNumber(String telNumber) {
this.telNumber = telNumber;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public int getActiveDeactive() {
return activeDeactive;
}
public void setActiveDeactive(int activeDeactive) {
this.activeDeactive = activeDeactive;
}
@Override
public int hashCode() {
int hash = 0;
hash += (userId != null ? userId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Users)) {
return false;
}
Users other = (Users) object;
if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.entity.Users[ userId=" + userId + " ]";
}
}