There is a simple PostgreSQL database. The database has a ready-made table with filled data. It is necessary to read data from the database, and display it through a servlet. The problem is that in all the examples @Entity creates tables, but they are already ready.

Question:

  • Are there annotations to help implement the configuration of the Entity to read from the database?

  • If not, what method to read?

Here is my code for general understanding.

 import org.hibernate.validator.constraints.NotEmpty; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import java.sql.Date; @Entity @Table(name = "rs_user") public final class UserEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) @NotEmpty @Column(name = "id_user") private long userId; @NotEmpty @Column(name = "user_name") private String userName; @NotEmpty @Column(name = "user_dismiss_date") private Date userDate; @NotEmpty @Column(name = "user_password_hash") private String passwordHash; @NotEmpty @Column(name = "user_password_salt") private String passwordSalt; @Column(name = "user_is_administrator") private boolean userAdmin; public long getUserId() { return userId; } public String getUserName() { return userName; } /** * This method returned of user data. * @return userDate of Date */ public Date getUserDate() { return userDate; } public String getPasswordHash() { return passwordHash; } public String getPasswordSalt() { return passwordSalt; } public boolean getUserAdmin() { return userAdmin; } } 

1 answer 1

I will throw you on the example of MySql

  UserEntity @Entity @Table(name = "rs_user") @NamedQueries({ @NamedQuery(name = "rs_user.findAll", query = "SELECT v FROM rs_user v"), } public class Hibernate { /** * SessionFactory configurator * @param dbUser user of Data Base * @param dbPassword password of Data Base * @param connectionHost connection of Data Base * @return SessionFactory */ public static SessionFactory configureSessionFactory(String dbUser, String dbPassword, String connectionHost) { Configuration configuration = new Configuration(); configuration.configure(new File("src/main/resources/hibernate/hibernate.cfg.xml")); if (dbUser != null && dbPassword != null && connectionHost != null) { configuration.setProperty("hibernate.connection.username",dbUser); configuration.setProperty("hibernate.connection.password",dbPassword); configuration.setProperty("hibernate.connection.url","jdbc:mysql://" + connectionHost + "/ctf"); } else new TestLogger().getLogger().warn("Cannot configuring Hibernate SessionFactory. Use default value"); SessionFactory sessionFactory = configuration.buildSessionFactory(); return sessionFactory; } } Hibernate.configureSessionFactory(.....).openSession(); List<UserEntity > v = (List<UserEntity >) session.getNamedQuery("rs_user.findAll").list(); 

This is what hibernate.cfg.xml looks like

  <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- DB details --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ctf</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <mapping class="main.java.DataFormat.hibernate.rs_user" /> </session-factory> </hibernate-configuration> 
  • and for what the minuses put ... - Senior Pomidor
  • 1. This is not an example for Spring; 2. You used proprietary Session instead of EntityManager ; 3. You duplicate a property in Java and XML configuration; - enzo