It's not a good idea, because everyone who has access to hibernate.cfg.xml file or pesistnce.xml file could read username, password and db hostname. But it's not the worst problem: the point is that you are binding the code to the DB. This is wrong, because you have to uncouple your code and the database. The customer must have the freedom to deploy your final application in whatever environment they want (e.g. development, test, integration, production) and each environment has got its own database. You can't edit the code every release you make.
You should declare in your hibernate.cfg.xml file and in your persistence.xml the jndi name of the datasource.
You can declare the datasource in your hibernate hibernate.cfg.xml file in the following way:
<hibernate-configuration>
<session-factory>
<!-- properties -->
<property name="connection.datasource">jdbc/DatasourceName</property>
<mapping class="MyEntity" />
</session-factory>
</hibernate-configuration>
You can declare the datasource name in your persistence.xml file in the following way:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="MyPU">
<jta-data-source>jdbc/DataSourceName</jta-data-source>
<class>MyEntity</class>
</persistence-unit>
</persistence>
Once you are done with declaring your datasource in your configurations file, you have to deploy the datasource in your Application Server. This procedure depends on which Application Server you are using (e.g. Glassfish, Tomcat etc.) and the DB you are using (e.g. MySQL, Postgres, etc.) For example, if your are using Glassfish and MySQL, you could follow this guide