简体   繁体   中英

Hibernate table mappings with annotations

I'm just getting started with Spring/Hibernate and I'm trying to understand how hibernate maps entities to tables. Right now I'm working with the following classes to test out CRUD operation with hibernate:

@Entity
@Table(name = "Users")
public class UserEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String firstName;
    private String lastName;
    private int age;

    //Getters, setters, etc.
}


@Repository
public class UserDAOImpl implements UserDAO {
    @Autowired
    HibernateTemplate hibernateTemplate;

    @Override
    public UserEntity getUser(String firstName, String lastName) {
        List<UserEntity> users = hibernateTemplate.find("from UserEntity u where u.firstName = ? and u.lastName = ?", firstName, lastName);
        return (users.size() == 0 ? null : users.get(0));
    }

    @Override
    public void saveOrUpdate(UserEntity user) {
        hibernateTemplate.saveOrUpdate(user);
    }
}

The code above is working fine, but I'm confused how UserEntity is being mapped to a table. More specifically, when calling hibernateTemplate.find("from UserEntity u where u.firstName = ? and u.lastName = ?", firstName, lastName) I get the following message in the log:

Hibernate: select userentity0_.id as id0_, userentity0_.age as age0_, userentity0_.firstName as firstName0_, userentity0_.lastName as lastName0_ from Users userentity0_ where userentity0_.firstName=? and userentity0_.lastName=?

When calling hibernateTemplate.find() I'm required to specify the table UserEntity whereas the log reports it's querying the table Users (as I specified with the @Table annotation).

Ideally I'd like to be consistent throughout my program and refer to the UserEntity table as Users rather than UserEntity . Is there some way I can do this? If so can I do it with HQL or would I need to write native SQL queries instead?

When you write HQL you are querying against the OO model. So in this case, your class name UserEntity is used. However, hibernate will translate this into a native SQL query and thus in the logs you will see references to users table. I'm not sure what your question is though. If the classname is UserEntity then throughout your application you should use UserEntity. I think that going into native SQL is only necessary when the HQL does not do what you want.

Vincent explained the matter, I'll just add some some clarifications.

The @Table annotation is optional, and merely indicates what the underlying table is (by default it is the same as the entity name (passing through a translation mechanism).

But the idea of hibernate (and orm layers) is to let the developer write code without any reference to the underlying relational model whatsoever. And that's what you should try to do - "forget" about the relational model beneath and use only the object model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM