many-to-one

Hibernate insert cascade not inserting foreign key

时间秒杀一切 提交于 2019-11-30 13:47:17
I have two entities: @Entity public class File ....... @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @OneToMany(fetch=FetchType.LAZY, mappedBy="file", cascade=CascadeType.ALL) private List<Tag> tags; ....... OTHER PROPERTIES ....... @Entity public class Tag ....... @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @ManyToOne @JoinColumn(name="file_id") private File file; @Column private String tag; ....... OTHER PROPERTIES ....... I am trying to insert into File (and subsequently Tag) by doing the following: File file = new File(); Tag tag = new Tag(); tag

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,

JPA many-to-one relation - need to save only Id

雨燕双飞 提交于 2019-11-30 02:08:02
I have 2 classes: Driver and Car. Cars table updated in separate process. What I need is to have property in Driver that allows me to read full car description and write only Id pointing to existing Car. Here is example: @Entity(name = "DRIVER") public class Driver { ... ID and other properties for Driver goes here ..... @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name = "CAR_ID") private Car car; @JsonView({Views.Full.class}) public Car getCar() { return car; } @JsonView({Views.Short.class}) public long getCarId() { return car.getId(); } public void setCarId(long carId) { this.car = new Car

Hibernate insert cascade not inserting foreign key

吃可爱长大的小学妹 提交于 2019-11-29 14:53:01
问题 I have two entities: @Entity public class File ....... @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @OneToMany(fetch=FetchType.LAZY, mappedBy="file", cascade=CascadeType.ALL) private List<Tag> tags; ....... OTHER PROPERTIES ....... @Entity public class Tag ....... @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @ManyToOne @JoinColumn(name="file_id") private File file; @Column private String tag; ....... OTHER PROPERTIES ....... I am trying to insert into

Can a @ManyToOne JPA relation be null?

放肆的年华 提交于 2019-11-29 12:27:19
问题 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

JPA Composite key with ManyToOne getting org.hibernate.PropertyAccessException: could not set a field value by reflection setter of

为君一笑 提交于 2019-11-29 09:46:49
I have a composite key ContractServiceLocationPK made out of three id's ( contractId , locationId , serviceId ) of type long in an embeddable class. The class which uses this composite key, ContractServiceLocation , maps these ids, using @MapsId annotation, to their objects. Here's how it looks like (removed setters/getters and irrelevant properties): Contract @Entity @Table(name = "Contract") public class Contract implements Serializable { public Contract() { } @Id @GeneratedValue private long id; @OneToMany(mappedBy = "contract", cascade = CascadeType.ALL, fetch= FetchType.EAGER) Collection

Hibernate Many-To-One Foreign Key Default 0

孤街醉人 提交于 2019-11-29 07:13:11
I have a table where the the parent object has an optional many-to-one relationship. The problem is that the table is setup to default the fkey column to 0. When selecting, using fetch="join", etc-- the default of 0 on the fkey is being used to try over and over to select from another table for the ID 0. Of course this doesn't exist, but how can I tell Hibernate to treat a value of 0 to be the same as NULL-- to not cycle through 20+ times in fetching a relationship which doesn't exist? <many-to-one name="device" lazy="false" class="Device" not-null="true" access="field" cascade="none" not

Hibernate ManyToOne vs OneToOne

孤街浪徒 提交于 2019-11-28 18:36:47
I can't see any difference in the schema of a Many-To-One relationship vs a OneToOne relationship: @Entity public class Order { @ManyToOne @JoinColumn(nullable = false) private Address address; vs @Entity public class Order { @OneToOne @JoinColumn(nullable = false) private Address address; Is there any difference? They look exactly the same on schema but there is difference on Hibernate Layer. If you try something like that: Address address = new Address(); Order order1 = new Order(); order1.setAddress(address); Order order2 = new Order(); order2.setAddress(address); save(); Everything will be

JPA delete all entites works strange

时光总嘲笑我的痴心妄想 提交于 2019-11-28 14:25:47
I have an entityrelation like this: In the ParentObj class: @OneToMany(mappedBy = "parentObj", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) private List<ChildObj> list; In the ChildObj class: @JoinColumn(name="PARENT_OBJ") @ManyToOne private ParentObj parentObj; When the parent object persisted or removed, the child is persisted/removed as well. BUT when I try to remove all the entities with a CriteriaDelete like: CriteriaDelete<ParentObj> query = builder.createCriteriaDelete(ParentObj.class); query.from(ParentObj.class); em.createQuery(query).executeUpdate(); or a

Mapping Sub-Classes by Type Table (Many-To-One Hibernate)

我是研究僧i 提交于 2019-11-28 10:42:31
问题 I have to make the domain-layer of a collage project. We have few standards, like we have to use Hibernate and the database also is fix. The relevant part of the database looks nearly like that: BusEntitys (Table 1) BusId Bus specific information BusType (Table 2) BusTypeId Seats ... SubClass Discriminator The problem I have is that there are 2 types of buses in the domain-layer distinguish by the discriminator in the BusType-table: @Entity class Bus { @Id int _id; ... } @Entity class