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.
Related
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
My question is does the cascaded class form part of the composite key? So for example if I am querying by Question answer will the result also consider the fields inside the cascaded class?
#Embeddable
public class QuestionAnswer implements Serializable {
#Column(name = "QUESTION_ID", nullable = false)
/** The id of the question this answer applies to. */
private Long questionId;
#Column(name = "ANSWER_ID", nullable = false)
/** The answer id. */
private Long answerId;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "PARTICIPANTS_REF_ID", referencedColumnName = "PARTICIPANT_REF_ID")
/** The participant that is involved in this answer.*/
private ParticipantReference participant
}
#Entity
#Table(name = "PARTICIPANTS_REF")
public class ParticipantReference implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "someIdGenerator")
#SequenceGenerator(name = "someIdGenerator", sequenceName = "SEQUENCE_GEN", allocationSize = 1)
#Column(name = "PARTICIPANT_REF_ID", nullable = false)
private Long id;
#Column(name = "AGE")
private long age;
#Column(name = "GENDER")
private String gender;
}
Short answer after comparing the hash of different objects is Yes the cascade class does form part of the composite key.
I am trying to make an one to many relationship between two tables, one of the two is the Aggregate root table (Vessel). But when I am creating a new entity the many to one table doesnt map its foreign key to the one to many table's primary key. What I am doing wrong?
Vessel Table (One to many)
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
#Table(name = "vessel_info")
#SequenceGenerator(name = "vessel_id_seq",sequenceName = "vessel_id_seq", initialValue = 1,allocationSize = 1)
public class Vessel {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator ="vessel_id_seq" )
#Column(name = "vessel_code")
private Long vesselCode;
private String name;
private Long companyId;
private Long imo;
private String type;
#Column(name = "fleet_id")
private Long fleetId;
private String yard;
private Integer hn;
private Date delivered;
private Double age;
#OneToMany(mappedBy = "vessel",cascade = CascadeType.ALL,
targetEntity = BoilerInfo.class, orphanRemoval = true,fetch = FetchType.EAGER)
private List<BoilerInfo> boilerInfo ;
BoilerInfo Table (Many to one)
#Data
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "boiler_info")
#SequenceGenerator(name = "boiler_id_seq",sequenceName = "boiler_id_seq", initialValue = 1,allocationSize = 1)
public class BoilerInfo {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator ="boiler_id_seq" )
private Long id;
private String maker;
private String type;
private String tubeType;
private String exhGasByPass;
#ManyToOne(fetch = FetchType.EAGER, optional = false,targetEntity = Vessel.class)
#JoinColumn(name="vessel_code",nullable = false)
private Vessel vessel;
}
CREATE VESSEL
private final VesselRepository vesselRepository ;
#Override
public Vessel create(Vessel entity) {
log.info("Creating {}.", entity);
Vessel vessel = vesselRepository.save(entity);
return vessel;
}
JSON POST
{
"fleetId":"1",
"name":"BOILERTEST",
"type":"temp",
"companyId":"1",
"boilerInfo":[{
"maker": "temp",
"type":"temp"
}]
}
The logs from the SQL query
Set both side of relation:
vesel.getBoilerinfos().add(boilerInfo);
boilerInfo.setVesel(vesel);
#Entity
#Table(name = "addition_type")
public class AdditionType {
#Id
#SequenceGenerator(name = "addition_type_id_seq", sequenceName = "addition_type_id_seq", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.AUTO, generator = "addition_type_id_seq")
#Column(name = "id")
private int id;
#Column(name = "code")
private String code;
#Column(name = "name")
private String name;
#Column(name = "sinhala_name")
private String sinhalaName;
#Column(name = "report_name")
private String reportName;
#Column(name = "status")
private int status;
#ManyToOne
#JoinColumn(referencedColumnName = "id")
private CompanyInfo companyInfo;
// there are not show getter and setter
}
When I use this class in Spring, two database tables were generated: the AdditionType table for the entity, and an addition_type_id_seq table corresponding to the sequence generator. The AdditionType table had the columns: id, name, code ..., and the other table had the next value of id.
When I use this class in Spring Boot it did not create two tables like that. Only the AdditionType table was created. Why is the SequenceGenerator not working in Spring Boot?
As MYSQL Dialect doesnt support sequences:
We can make use of Table Generator strategy to define the sequences :
#Id
#TableGenerator(name = "addition_type_id_seq", allocationSize = 1, table = "ADDITION_TYPE_SEQUENCES",
pkColumnName = "SEQ_NAME",
valueColumnName = "SEQ_NUMBER",
pkColumnValue = "SEQUENCE")
#GeneratedValue(strategy = GenerationType.TABLE, generator = "addition_type_id_seq")
#Column(name = "id")
private int id;
This will ensure creating two tables : ADDITION_TYPE_SEQUENCES and ADDITION_TYPE
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");