I have the table
#Entity
#Table(name="cash")
public class CashItem extends Item {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO, generator = "cash_seq_gen")
#SequenceGenerator(name = "cash_seq_gen", sequenceName = "cash_seq")
private Long id;
#Column(name="object_id")
private Long objectId = 1l;
#Column(name="object_type")
private Long objectType = 1l;
And the link table
#Entity
#Table(name="cash_to_order")
#SuppressWarnings("serial")
public class CashToOrderItem extends Item {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.AUTO, generator = "cash_to_order_seq_gen")
#SequenceGenerator(name = "cash_to_order_seq_gen", sequenceName = "cash_to_order_seq")
Long id;
#ManyToOne
#JoinColumn(name="order_id",referencedColumnName="id")
private OrderItem order;
#ManyToOne
#JoinColumn(name="cash_id",referencedColumnName="id")
private CashItem cash;
I get Criteria from CashToOrderItem and wanna make next query
criteria.createAlias("cash","c1");
criteria.add(Restrictions.eq("c1.objectType",ocf.getCounterparty()));
criteria.add(Restrictions.eq("с1.objectId",ocf.getSubCounterparty()));
But error occured what could not resolve property: с1 of: CashToOrderItem if i added two or more Restrictions.
If you are creating criteria on CashToOrder, please reach out to cash from that class for alias. i.e. cashToOrder.cash
Criteria criteria = session.createCriteria(CashToOrderItem.class, "cashToOrder");
criteria.createAlias("cashToOrder.cash","c1");
Related
I have a table schema like this. Notification can be of 2 types : twitter or fb
For each notification, there will be 1 row in notification table and 1 in either twitter_post/ fb_post.
Each table have auto sequence id generator.
FBPost and TwitterPost is mapped via notification_id (PK)
I have created entity class like this:
#Entity
#Table(name = "notification")
public class Notification {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "not_id_sequence")
private String id;
#Column(name = "description")
private String description;
#OneToOne(mappedBy = "notification")
private FBPost fbPost;
#OneToOne(mappedBy = "notification")
private TwitterPost twitterPost;
}
#Entity
#Table(name = "fb_post")
public class FBPost {
#Id
#Column(name = "post_id")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_fid_sequence")
private Long postId;
#Column(name = "post_content")
private String postContent;
#OneToOne
private Notification notification;
}
#Entity
#Table(name = "twitter_post")
public class TwitterPost {
#Id
#Column(name = "post_id")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_tid_sequence")
private Long postId;
#Column(name = "post_content")
private String postContent;
#OneToOne
private Notification notification;
}
I am using JpaRepository to save into the database. But not getting any row into fb_post or twitter_post table.
Am i doing it wrong in entity class.
Try adding #JoinColumn(name = "notification_id", referencedColumnName = "notification_id") below #oneToOne in TwitterPost and FBPost class for the notification variable
Looks like you are missing cascade. Try to add cascade = CascadeType.ALL into #OneToOne of the parent entity - Notification:
#Entity
#Table(name = "notification")
public class Notification {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "not_id_sequence")
private String id;
#Column(name = "description")
private String description;
#OneToOne(mappedBy = "notification", cascade = CascadeType.ALL)
private FBPost fbPost;
#OneToOne(mappedBy = "notification", cascade = CascadeType.ALL)
private TwitterPost twitterPost;
}
For more information I would recommend to read hot to map one-to-one relationship.
so I searched for the answers for my problem on the internet but didn't find something that helped, basically a need to have a ManyToOne Relationship between two classes, of which one of them has an EmbeddedId, I'm going to leave the code here and the error message that it gives (I'm using wildfly to run the server).
public class InventoryPK implements Serializable {
#ManyToMany(cascade = CascadeType.ALL)
#JoinColumn(name = "user_id")
private Item itemId;
#ManyToOne
#JoinColumn(name="CD_EMPRESA")
private Company company;
}
#Entity
#Table(name = "inventario", schema = "mxnextmob")
public class Inventory extends BaseModel {
#EmbeddedId
private InventoryPK id;
#SequenceGenerator(schema = "mxnextmob", name = "inventory_sequence", sequenceName = "inventory_sequence", allocationSize = 1, initialValue = 1)
#GeneratedValue(strategy = GenerationType.AUTO, generator = "inventory_sequence")
private Integer inventory;
#Column
private BigDecimal quantity;
#Column
private BigDecimal weight;
}
public class Company extends BaseModel {
#Id
#SequenceGenerator(schema = "mxnextmob", name = "company_sequence", sequenceName = "company_sequence", allocationSize = 1, initialValue = 1)
#GeneratedValue(strategy = GenerationType.AUTO, generator = "company_sequence")
private Integer code;
#Column
private String name;
#OneToMany(mappedBy = "company")
private List<UserSeller> userSeller;
#OneToMany(mappedBy = "id.company")
private List<Inventory> inventories;
}
and the error is as follows:
service jboss.persistenceunit."mxnext-mobile.war#mxnextmobileDS":
org.hibernate.AnnotationException: mappedBy reference an unknown
target entity property:
br.com.maxiconsystems.mobile.model.Inventory.company in
br.com.maxiconsystems.mobile.model.Company.inventory
There are a few ways to map what you seem to have as a table, but I'd recommend Inventory be changed to something like:
public class Inventory extends BaseModel {
#Id
#SequenceGenerator(schema = "mxnextmob", name = "inventory_sequence", sequenceName = "inventory_sequence", allocationSize = 1, initialValue = 1)
#GeneratedValue(strategy = GenerationType.AUTO, generator = "inventory_sequence")
private Integer inventory;
#Embedded
private InventoryPK alternateKey;
#Column
private BigDecimal quantity;
#Column
private BigDecimal weight;
}
This allows you to use the inventory Integer as its primary key; this simplifies any future references you may need to add to Inventory, as foreign keys would be required in JPA to reference all its ID columns.
I have the following classes and I'm trying to store all the derived classes in one table (aircraft).
But I get the above error, of course, I was searching for a solution for a while, and tried to play with the mappedBy name value as well...
What am I missing?
#Entity // for hibernate
#Table // for database
public class Airline {
#Id
#SequenceGenerator(
name = "airline_sequence",
sequenceName = "airline_sequence",
allocationSize = 1
)
#GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "airline_sequence"
)
private long airline_id;
#ManyToOne
#JoinColumn(name = "airport_id")
private Airport homeAirport;
#OneToMany(mappedBy = "aircraft", cascade = CascadeType.ALL)
private List<Aircraft> airplaneList;
#Inheritance
#DiscriminatorColumn
#Entity
public abstract class Aircraft implements Serializable {
#Id
#SequenceGenerator(
name = "aircraft_sequence",
sequenceName = "aircraft_sequence",
allocationSize = 1
)
#GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "aircraft_sequence"
)
// Data members
private long aircraft_id
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "airline_id")
private Airline airline;
#Version
private Long version;
#Entity // for hibernate
#Table // for database
#DiscriminatorValue("Airplane")
public class Airplane extends Aircraft {
//Constructors here
#Entity
#Table(name = "CONTACT_GROUP")
#JsonInclude(JsonInclude.Include.NON_NULL)
public class ContactGroup implements Serializable
{
private static final long serialVersionUID = 7161778136151592279L;
#Id
#GenericGenerator(name = "increment", strategy = "increment")
#GeneratedValue(generator = "increment")
#Column(name = "GRPOUP_ID")
private Long id;
#OneToMany(mappedBy="contactGroup",cascade = { CascadeType.ALL }, targetEntity = Contact.class)
Set<Contact> contacts;
}
#Entity
#Table(name = "CONTACT")
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Contact implements Serializable
{
private static final long serialVersionUID = 7161778136151592279L;
#Id
#GenericGenerator(name = "increment", strategy = "increment")
#GeneratedValue(generator = "increment")
#Column(name = "CONTACT_ID")
Long id;
#GeneratedValue(generator="system-uuid")
#GenericGenerator(name="system-uuid", strategy = "uuid2")
#Column(name="group", unique=true)
String group;
#ManyToOne(cascade={CascadeType.ALL})
#JoinColumn(name = "GRPOUP_ID")
ContactGroup contactGroup;
}
Can you please tell me what I wrong in the above code not getting autoincreament
gives ORA-00001: unique constraint violated
When i run the test case again and again
And I how to generate the uuid value it was inserted in blank vlaue in the first record
Can you please tell me Thanks in Advance
Due to you are working with Oracle DB, you could define and use a Oracle's Sequence to get a uuid doing something like next:
Create the sequence in your database,
CREATE SEQUENCE "CONTACT_GROUP_SEQUENCE"
START WITH 1
INCREMENT BY 1 -- < here is your "autoincrement" simulation
MAXVALUE 999999999
MINVALUE 1
NOCACHE
ORDER
Map the sequence with
#SequenceGenerator and use it inplace of #GenericGenerator
public class ContactGroup implements Serializable
{
#Id
#SequenceGenerator(name = "CONTACT_GROUP_SQ", sequenceName = "CONTACT_GROUP_SEQUENCE", allocationSize = 0)
#GeneratedValue(generator = "CONTACT_GROUP_SQ", strategy = GenerationType.SEQUENCE)
#Column(name = "GRPOUP_ID")
private Long id;
...
You can't find this and another examples about how to use TableGenerator with Oracle DB in this link to Oracle documentation.
Hibernate cascade remove doesn't remove child when deleting parent.
causing this error:
ORA-02292: violated integrity constraint (owner.constraintname)- child record found.
Here are my entities:
#Entity
#Table(name = "FLIGHT_MAP_REGION")
#Audited
public class FlightMapRegion extends AbstractBWSModel {
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "FLIGHT_MAP_REGION_ID_GENERATOR", sequenceName = "SEQ_FLIGHT_MAP_REGION", allocationSize=1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FLIGHT_MAP_REGION_ID_GENERATOR")
private Long ID;
#Column(name = "CODE")
private String code;
// bi-directional one-to-many association to FlightMapRegionI18n
#OneToMany (mappedBy = "flightMapRegion", fetch = FetchType.EAGER, cascade=CascadeType.REMOVE, orphanRemoval=true)
#Fetch(value = FetchMode.SUBSELECT)
#NotAudited
private List<FlightMapRegionI18n> flightMapRegionI18ns;
//getters&setters
and:
#Entity
#Table(name = "FLIGHT_MAP_REGION_I18N")
#Audited
public class FlightMapRegionI18n extends AbstractBWSModel implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#SequenceGenerator(name = "FLIGHT_MAP_REGION_I18N_ID_GENERATOR", sequenceName = "SEQ_FLIGHT_MAP_REGION_I18N", allocationSize=1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "FLIGHT_MAP_REGION_I18N_ID_GENERATOR")
private Long ID;
#Column(name = "NAME")
private String name;
// bi-directional many-to-one association to flightMapRegion
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "FLIGHT_MAP_REGION_CODE", referencedColumnName="CODE")
#AuditJoinTable
private FlightMapRegion flightMapRegion;
//getters & setters
I want to delete any child FlightMapRegionI18n records when deleting the parent FlightMapRegion record.
Note that these tables are not connected via id, they are connected like this:
FOREIGN KEY (FLIGHT_MAP_REGION_CODE)
REFERENCES FLIGHT_MAP_REGION (CODE)
And I don't want to add ON DELETE CASCADE on database level. I want to achive this programmatically in java.