Create session factory in Hibernate 4

Yes, they have deprecated the previous buildSessionFactory API, and it’s quite easy to do well.. you can do something like this.. EDIT : ServiceRegistryBuilder is deprecated. you must use StandardServiceRegistryBuilder public void testConnection() throws Exception { logger.info(“Trying to create a test connection with the database.”); Configuration configuration = new Configuration(); configuration.configure(“hibernate_sp.cfg.xml”); StandardServiceRegistryBuilder ssrb = new … Read more

TransactionRequiredException Executing an update/delete query

I am not sure if this will help your situation (that is if it stills exists), however, after scouring the web for a similar issue. I was creating a native query from a persistence EntityManager to perform an update. Query query = entityManager.createNativeQuery(queryString); I was receiving the following error: caused by: javax.persistence.TransactionRequiredException: Executing an update/delete … Read more

New object with HQL

I think that the section 15.6. The select clause covers what you’re trying to achieve: 15.6. The select clause … Queries can return multiple objects and/or properties as an array of type Object[]: select mother, offspr, mate.name from DomesticCat as mother inner join mother.mate as mate left outer join mother.kittens as offspr Or as a … Read more

Registering a SQL function with JPA and Hibernate

You might read articles telling you to register the SQL function by extending the Hibernate Dialect, but that’s a naive solution. Since Hibernate ORM 5.2.18 and 5.3.1, the best way to register a SQL function is to supply a MetadataBuilderContributor like this: public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor { @Override public void contribute(MetadataBuilder metadataBuilder) { metadataBuilder.applySqlFunction( … Read more

JPA Map mapping

Although answer given by Subhendu Mahanta is correct. But @CollectionOfElements is deprecated. You can use @ElementCollection instead: @ElementCollection @JoinTable(name=”ATTRIBUTE_VALUE_RANGE”, joinColumns=@JoinColumn(name=”ID”)) @MapKeyColumn (name=”RANGE_ID”) @Column(name=”VALUE”) private Map<String, String> attributeValueRange = new HashMap<String, String>(); There is no need to create a separate Entity class for the Map field. It will be done automatically.

How do I map a BigDecimal in Hibernate so I get back the same scale I put in?

Just for informational sake, I can tell you that the creation of the BigDecimal coming back from the database is done by the proprietary JDBC driver’s implementation of the ‘getBigDecimal’ method of the database-specific ‘ResultSet’ sub-class. I found this out by stepping thru the Hibernate source code with a debugger, while trying to find the … Read more

maven missing dependency jta-1.0.1b

<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.3.2.ga</version> </dependency> it has a recommended dependency for JTA 1.1. OR You can add “http://download.java.net/maven/2” as new repository in pom.xml or settings.xml <repository> <id>java.net.m2repo</id> <name>java.net Maven 2 Repository</name> <url>http://download.java.net/maven/2/</url> <layout>default</layout> </repository> For any JEE library provided by Sun or oracle .. Add below repository <repositories> <repository> <id>GlassFish</id> <name>GlassFish Maven Repository</name> <url>http://download.java.net/maven/glassfish/</url> </repository> … Read more

Autowired Repository is Null in Custom Constraint Validator

Which ValidatorFactory are you using. Or to put it another way, hot to you bootstrap Bean Validation? Per default Bean Validation (and Hibernate Validator as reference implentation) does not inject dependencies into ConstraintValidator instances. At least in Bean Validation 1.0. To enable dependency injection you have to configure a custom ConstraintValidatorFactory. Spring offers SpringConstraintValidatorFactory which … Read more

UnsupportedOperationException: The application must supply JDBC connections

Wow, just fixed the problem. sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry()); I was missing the .applySettings(configuration.getProperties()) Learnings configure() should be called AFTER setProperty Use hibernate.connection.url and NOT connection.url if you use hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect Set log4j property for hibernate logs to ALL, so that you can see more detailed issues To get rid of the WARN Recognized obsolete hibernate … Read more