hibernate-mapping

How to make a one-to-many bidirectional relation with JPA annotations, where the collection side owns it

不羁的心 提交于 2019-12-01 23:29:45
问题 I have from the Hibernate docs a mapping in XML. My question is how to do it with JPA annotations. It is a one-to-many bidirectional relation, and the collection side owns it. Thanks. <class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <set name="addresses" table="PersonAddress"> <key column="personId"/> <many-to-many column="addressId" unique="true" class="Address"/> </set> </class> <class name="Address"> <id name="id" column="addressId"> <generator class

Hibernate: many-to-many relationship table as entity

此生再无相见时 提交于 2019-12-01 20:50:25
问题 The question is in title: How can I make many-to-many relationship table as entity? 回答1: I would say, that your question is very reasonable. Take a look at this doc part: Chapter 24. Best Practices. An Extract: Do not use exotic association mappings: Practical test cases for real many-to-many associations are rare. Most of the time you need additional information stored in the "link table". In this case, it is much better to use two one-to-many associations to an intermediate link class. In

How To Use Sequence In Hibernate As A Property In XML Mapping

谁都会走 提交于 2019-12-01 19:32:51
How do I use a sequence in Hibernate XML mappings? The documentation mentions the <generator> element. However, I want the sequence to be a column instead of an ID. Karl Walsh I know when using Hibernate with Oracle the id in the mapping file is defined something like: <id name="id" column="item_id"> <generator class="sequence"> <param name="sequence">NAME_OF_YOUR_SEQUENCE</param> </generator> </id> You can also specify the generator class as "native", which is handy if you then switch to an auto incrementing RDMS such as MySQL. The sequence bit is then ignored in MySQL. Edit: Just re-read

@OrderBy causes java.lang.ClassCastException: antlr.CommonToken cannot be cast to antlr.Token [duplicate]

梦想的初衷 提交于 2019-12-01 18:44:40
问题 This question already has answers here : @OrderBy causing org.hibernate.HibernateException: Unable to parse order-by fragment (3 answers) Closed 6 months ago . I'm trying to use the @OrderBy annotation on a Spring MVC project that run on a JBoss EAP 6.1+ server. I have 2 projects: the first one is named model-gen-wifipnsd and it contains only the model classes that are used by the second project (named WIFIPNSD ) that represent the web application. 1) So for the model-gen-wifipnsd I have this

@OrderBy causes java.lang.ClassCastException: antlr.CommonToken cannot be cast to antlr.Token [duplicate]

本小妞迷上赌 提交于 2019-12-01 18:12:47
This question already has an answer here: @OrderBy causing org.hibernate.HibernateException: Unable to parse order-by fragment 3 answers I'm trying to use the @OrderBy annotation on a Spring MVC project that run on a JBoss EAP 6.1+ server. I have 2 projects: the first one is named model-gen-wifipnsd and it contains only the model classes that are used by the second project (named WIFIPNSD ) that represent the web application. 1) So for the model-gen-wifipnsd I have this pom.xml file: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi

Java Hibernate Mapping Exception! (Could not determine type for: java.util.Map)

人盡茶涼 提交于 2019-12-01 17:28:55
I have made a class with name of Movie with folowing fields: @Id @GeneratedValue private Long id; private String name; @ElementCollection(targetClass = String.class) private Map<String, String> properties; private Double rate; private Integer votersCount; private Date releaseDate; private Integer runtime; @ManyToMany @JoinTable(name = "movie_director") @IndexColumn(name = "directorIndex") private List<Person> directors; @ManyToMany @JoinTable(name = "movie_writer") @IndexColumn(name = "writerIndex") private List<Person> writers; @OneToMany @IndexColumn(name = "roleIndex") private List

Is it possible to have foreign key enforced without object-to-object mapping?

这一生的挚爱 提交于 2019-12-01 17:20:23
Assuming the following mappings are provided: <class name="A" table="a_table"> <id name="id"/> <many-to-one name="entityB" column="fk_B" not-null="false" unique="true"/> </class> <class name="B" table="b_table"> <id name="id"/> </class> Java class: public class A { private long id; private B entityB; // getters and setters skipped } Is it possible to change the Hibernate mapping so that foreign key is still enforced and created by Hibernate upon startup , but class A would look like as the following: public class A { private long id; private long idOfB; // getters and setters skipped } I

Can hibernate scan packages to create SessionFactory automatically?

人走茶凉 提交于 2019-12-01 15:41:48
问题 Can I configure Hibernate to scan packages automatically to create a SessionFactory from @Entity annotated beans ? Currently I am using Configuration config = new Configuration() ; config.addAnnotatedClass(MyEntity.class); I do not want to use hibernate.cfg.xml to configure mappings. Please note I want to achieve this in a plain Java project without using any Spring or such frameworks. Similar question have been answered using Spring before but I want to achieve it without using Spring or

mapping multiple sets in one table in hibernate

只谈情不闲聊 提交于 2019-12-01 15:28:01
I've got a User an a Log class (which I cannot change): class User { private long id; private String name; private Set<Log> accessLogs; private Set<Log> actionLogs; } class Log { private String what; private Date when; } A possible mapping will look like: <class name="com.example.User" table="users"> <id name="id" access="field"> <generator class="native" /> </id> <property name="name" length="256" /> <set name="accessLogs" table="user_access_logs" cascade="all-delete-orphan" order-by="`when`"> <key column="user_id" /> <composite-element class="com.example.Log"> <property name="what" length=

Mapping a property to a field from another table in NHibernate

懵懂的女人 提交于 2019-12-01 12:28:31
问题 consider the following class: class Order { int OrderId {get; set;} int CustomerId {get; set;} string CustomerName {get; set;} //other fields go here } which is mapped to Orders table. Is it possible to map the property CustomerName to the Customers table through the foreign key relation? 回答1: Yes, you can use the join mapping element for this. Another option is to map a view instead of a table. But if possible you should take the object-oriented approach and map the many-to-many relationship