Instantiating a context in LINQ to Entities

Creating a new ObjectContext each time does involve ‘some’ overhead. Essentially the overhead involved is copying metadata from a global cache into metadata associated with the specific ObjectContext. This overhead is relatively minor, so often it is not worth worrying about, especially when you consider the extra safety inherent in the using pattern. For me … Read more

org.hibernate.MappingException: Unknown entity: annotations.Users

The Hibernate configuration file must define the entity classes: <mapping class=”annotations.Users”/> Or you must explicitly add the class to the configuration using configuration.addClass(annotations.Users.class) // Read mappings as a application resourceName // addResource is for add hbml.xml files in case of declarative approach configuration.addResource(“myFile.hbm.xml”); // not hibernateAnnotations.cfg.xml

JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically

There are several way to solve it: As described in Do I need <class> elements in persistence.xml?, you can set hibernate.archive.autodetection property and Hibernate should be able to look up all annotated classes from classpath. However, that’s not JPA spec compliant. If you are using Spring, from Spring 3.1.2 (or probably even a bit earlier), … Read more

How to set a default entity property value with Hibernate

If you want a real database default value, use columnDefinition: @Column(name = “myColumn”, nullable = false, columnDefinition = “int default 100”) Notice that the string in columnDefinition is database dependent. Also if you choose this option, you have to use dynamic-insert, so Hibernate doesn’t include columns with null values on insert. Otherwise talking about default … Read more

JPA Entity as JSF Bean?

You could do so. It’s technically possible. But it does (design)functionally not make any sense. You’re basically tight-coupling the model with the controller. Usually the JPA entity (model) is a property of a JSF managed bean (controller). This keeps the code DRY. You don’t want to duplicate the same properties over all place, let alone … Read more