Это мой Users.javaHibernate - NullPointerException при получении списка/Set
package com.serwis.model;
import javax.persistence.*;
import java.util.Arrays;
import java.util.Set;
@Entity
@Table(name="Users")
public class Users {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="login")
private String login;
@Column(name="password")
private String password;
@Column(name="email")
private String email;
@Column(name="avatar")
private byte[] avatar;
@OneToMany(mappedBy="users", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private Set<Post> posts;
public Users(){}
public Users(int id, String login, String password, String email, byte[] avatar) {
this.id = id;
this.login = login;
this.password = password;
this.email = email;
this.avatar = avatar;
}
public Users(byte[] avatar, String email, String password, String login) {
this.avatar = avatar;
this.email = email;
this.password = password;
this.login = login;
}
@Override
public String toString() {
return "Users{" +
"id=" + id +
", login='" + login + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", avatar=" +
", posts=" + posts.size()+
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public byte[] getAvatar() {
return avatar;
}
public void setAvatar(byte[] avatar) {
this.avatar = avatar;
}
}
Post.java
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name="Post")
public class Post {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="content")
private String content;
@Column(name="date")
private Date date;
@Column(name="tags")
private String tags;
@ManyToOne
@JoinColumn(name="id")
private Users users;
public Post(){}
public Post(String content, Date date, String tags) {
this.content = content;
this.date = date;
this.tags = tags;
}
public Post(String content, Date date, String tags, Users users) {
this.content = content;
this.date = date;
this.tags = tags;
this.users = users;
}
@Override
public String toString() {
return "Post{" +
"id=" + id +
", content='" + content + '\'' +
", date=" + date +
", tags='" + tags + '\'' +
", users=" + users +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Users getUsers() { return users; }
public void setUsers(Users users) { this.users = users; }
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
}
Test.java пакет com.serwis;
import com.serwis.model.Chat;
import com.serwis.model.Photo;
import com.serwis.model.Post;
import com.serwis.model.Users;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import java.util.List;
/**
* Created by Jodanpotasu on 2016-07-17.
*/
public class TEST {
public static void main(String[] args){
Configuration configuration = new Configuration().configure().addAnnotatedClass(Users.class).addAnnotatedClass(Post.class);
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// builds a session factory from the service registry
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
try{
Users user = new Users(null,"YYYYYYYYYYYYY","dupa","dupa");
Post post = new Post("content",null,"tags",user);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.save(post);
Query query = session.createQuery("from Users");
List list = query.list();
Query query1 = session.createQuery("from Post");
List list1 = query1.list();
session.getTransaction().commit();
session.close();
System.out.println(list);
System.out.println("post");
System.out.println(list1);
}catch (Exception e){
System.out.println("blad\n\n");
System.out.println(e);
} finally {
System.out.println("zrobiono");
}
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC connection settings -->
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:file:database/serwisDB;hsqldb.write_delay=false;shutdown=true;</property> <!--shutdown=true; -->
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!--https://www.progress.com/jdbc/resources/tutorials/connection-pooling/connection -->
<property name="connection.pool_size">1</property>
<!-- SQL DIALECT -->
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- SHOW SQL OUTPUT -->
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Выход из консоли
java.lang.NullPointerException
zrobiono
22:40:25.391 [pool-1-thread-1] DEBUG org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl - Connection pool now considered primed; min-size will be maintained
Process finished with exit code -1
Моя проблема заключается в том, что я не понимаю, почему он не работает, где у меня есть какая-то ошибка. Я пытался изменить Set to List, позже FetchType.EAGER и т. Д., Но ничего не работает.
Состав: < 1, многие> - Люди, Post
я не видел хороший учебник по новейшей Hibernate :(