hibernate-mapping

Can a @ManyToOne JPA relation be null?

寵の児 提交于 2019-11-30 08:34:13
I have a table that has foreign key of another table (many to one relationship) but i want it to be nullable. Something like this: public class SubType() { @Id @GeneratedValue(generator = "system-uuid") @GenericGenerator(name = "system-uuid", strategy = "uuid") private String id; } public class TopUp { @Column(nullable = true) @ManyToOne(optional = false, fetch = FetchType.LAZY) private SubType subType; } But @Column(nullable = true) throws the NullPointerException and says subtype can not be null. Is there any way to get the ManyToOne accept null? You need to set: @ManyToOne(optional = true,

How to do multiple column UniqueConstraint in hbm?

对着背影说爱祢 提交于 2019-11-30 07:19:49
问题 Working on some legacy hibernate code. How do I do the following with hbm.xml(hibernate mapping file) instead of with annotations? @Table(name="users", uniqueConstraints = { @UniqueConstraint(columnNames={"username", "client"}), @UniqueConstraint(columnNames={"email", "client"}) }) public class User implements Serializable { private static final long serialVersionUID = 1L; @Id private int id; private String username; private String email; private Client client; } 回答1: Use the properties tag:

How can I map “insert='false' update='false'” on a composite-id key-property which is also used in a one-to-many FK?

心已入冬 提交于 2019-11-30 07:18:18
问题 I am working on a legacy code base with an existing DB schema. The existing code uses SQL and PL/SQL to execute queries on the DB. We have been tasked with making a small part of the project database-engine agnostic (at first, change everything eventually). We have chosen to use Hibernate 3.3.2.GA and "*.hbm.xml" mapping files (as opposed to annotations). Unfortunately, it is not feasible to change the existing schema because we cannot regress any legacy features. The problem I am

How can I mark a foreign key constraint using Hibernate annotations?

冷暖自知 提交于 2019-11-30 06:14:28
问题 I am trying to use Hibernate annotation for writing a model class for my database tables. I have two tables, each having a primary key User and Question. @Entity @Table(name="USER") public class User { @Id @Column(name="user_id") @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @Column(name="username") private String username; // Getter and setter } Question Table. @Entity @Table(name="QUESTION") public class Questions extends BaseEntity{ @Id @Column(name="question_id")

To map a database view with no primary key, in hibernate xml mapping

久未见 提交于 2019-11-30 04:59:27
问题 I have created a view which will be used for fetching the data only(readonly) View : Grid_View > My Hibernate hbm file <hibernate-mapping> <class name="hibernate.domain.View" table="Grid_View" mutable="false"> <property name="ACCT_BR_CD" type="string"> <column name="ACCT_BR_CD"/> </property> <property name="ACCT_NO" type="string"> <column name="ACCT_NO"/> </property> <property name="LGL_ENTY_NM" type="string"> <column name="LGL_ENTY_NM"/> </property> <property name="CUST_CTRY_CD" type="string

Map entity using query in Hibernate

懵懂的女人 提交于 2019-11-30 04:04:02
consider table sales (id, seller_id, amount, date) and here is a view that is generated from sales using query SELECT seller_id, SUM(amount) FROM sales GROUP BY seller_id total_sales (seller_id, amount) I want to make an entity for total sales but without the view on the sql side. This entity will be constructed from a query. The closest thing I found is this , but I could not make it work. Even if I define the loader, hibernate looks for the entity's table and gives an error if it cannot find it. If I create the table it does not load the entity from the named query I defined, Hibernate

HIbernate Mapping Exception: PropertyNotFoundException: Could not find a setter

笑着哭i 提交于 2019-11-30 03:21:36
问题 I have two POJO's ,STOCK and STOCK_DETAILS (one to many relationship). Also I have one interface IAUDITLOG (having two methods). I need to implement this interface with BOTH POJO's and want to write some implementation within those methods. But when I implement IAUDITLOG interface with child class "STOCKDETAILS" then it gives exception "that you should have setter property" STOCK CLASS: @Entity @Table(name = "stock") public class Stock implements java.io.Serializable, IAuditLog { @Id

remove collection with delete-orphan not work with null assignment? :(

℡╲_俬逩灬. 提交于 2019-11-29 21:18:01
问题 I am having problems removing another entity through cascade delete-orphan. It works when I clear the associated set collection, but not when I make the set collection null. Let me explain in detail. The config snippet: <class name="com.sample.CategoriesDefault" table="cats"> <id name="id" column="id" type="string" length="40" access="property"> <generator class="assigned" /> </id> <version name="version" column="objVrs" unsaved-value="negative"/> <set name="bla" lazy="false" cascade="all

JPA enumerated types mapping. Best approach

最后都变了- 提交于 2019-11-29 20:26:14
As usual Java enum types have corresponding codes and name description. And Java classes that contain such fields, contain them as Enum: public enum MyEnum{ SOMEINSTANCE(1, "test1"), SOMEINSTANCE(2, "test2"); private final int code; private final String name; private MyEnum(int code, String name){ this.code = code; this.name = name; } ... helper getter for code and name } @Entity puclic class EnumHolder{ private MyEnum myEnum; } I'm a newbie to JPA, but I wish to have the ' myEnums ' table, looked like: code int not null, name varchar(50) not null) And in my enumHolder table I want to have the

@ManyToOne and @BatchSize

半城伤御伤魂 提交于 2019-11-29 14:38:39
问题 I found in some old code strange thing (at least for me). The field which is annotated @ManyToOne is also annotated with @BatchSize . I always thought that @BatchSize annotation only affects when annotated at class level or on a collection ( @OneToMany ) and affects pre-fetching when iterating. But maybe I am wrong and annotating @ManyToOne with @BatchSize affects something. I can't find the answer in the documentation. Does annotating @ManyToOne with @BatchSize have sense? 回答1: @ManyToOne