0

Below code uses JPA and Spring and can create a row in the table "games".

@Repository
@Transactional
public class GameDao {
    /**
     * Save the game in the database.
     */
    public void create(Game game) {
        entityManager.persist(game);
        return;
    }

    // An EntityManager will be automatically injected from entityManagerFactory
    // setup on DatabaseConfig class.
    @PersistenceContext
    private EntityManager entityManager;

} 

Looking at this create function, I wonder how does it find the table that it is supposed to insert?

If it can just find out the table from the argument type (Game game), can I create a class called CreateDao and do all the create operations from there?

brainmassage
  • 1,234
  • 7
  • 23
  • 42
  • 1
    Not sure that I fully understand the question. It finds the table because Game is added in the persistence.xml file and it is annotated with `@Entity` and probably something like `@Table(name = "game_tbl")`. In theory, yes, you could create 1 general DAO but you would have to rely heavily on Java Generics. – Nico Van Belle Mar 03 '17 at 12:45
  • Answering to your second question, you can find a good example in this blog: [NetBeans to Generate Simpler RESTful Web Services](https://dzone.com/articles/nb-generate-simpler-rest). Use it or see the implementation of `AbstractFacade` class – perissf Mar 03 '17 at 12:51
  • Possible duplicate of [Single DAO & generic CRUD methods (JPA/Hibernate + Spring)](http://stackoverflow.com/questions/3888575/single-dao-generic-crud-methods-jpa-hibernate-spring) – Alan Hay Mar 03 '17 at 13:43

1 Answers1

2

Look at the source of the Game class - it should be annotated with @Entity and probably @Table - which defines the actual DB table.

So, the answer is no, it's not the classes name - GameDao that does the wiring.

Eugene
  • 117,005
  • 15
  • 201
  • 306