我是JPA的新手。需要将数据插入两个数据库表(两个表不同,这意味着没有外键关系)。我正在使用POSTMAN从URL和有效负载主体部分发送json数据。 下面显示了两个实体类。
USERREDIRECT.JAVA `
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.QueryHint;
import javax.persistence.Table;
import org.codehaus.jackson.annotate.JsonProperty;
@Entity
@Cacheable(false)
@NamedQueries({
@NamedQuery(name = "getuserRedirectById", query = "SELECT a FROM userRedirect a WHERE a.redirectId=:redirectId", hints = @QueryHint(name = "eclipselink.refresh", value = "true"))
,
@NamedQuery(name = "getAlluserRedirect", query = "SELECT a FROM userRedirect a order by a.redirectId", hints = @QueryHint(name = "eclipselink.refresh", value = "true"))})
@Table(name = "userRedirect")
public class userRedirect {
@Id
@Column(name = "redirect_id")
@JsonProperty("redirect_id")
private String redirectId;
@Column(name = "loop_ind")
@JsonProperty("loop_ind")
private String loopInd;
@OneToMany(mappedBy = "userRedirectEntity", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<userRedirectRicTgrpDetails> userRedirectRicTgrpDetailsList;
public userRedirect() {
}
public String getLoopInd() {
return loopInd;
}
public void setLoopInd(String loopInd) {
this.loopInd = loopInd;
}
public List<userRedirectRicTgrpDetails> getuserRedirectRicTgrpDetailsList() {
return userRedirectRicTgrpDetailsList;
}
public void setuserRedirectRicTgrpDetailsList(List<userRedirectRicTgrpDetails> userRedirectRicTgrpDetailsList) {
this.userRedirectRicTgrpDetailsList = userRedirectRicTgrpDetailsList;
}
/**
* @return the redirectId
*/
public String getRedirectId() {
return redirectId;
}
/**
* @param redirectId the redirectId to set
*/
public void setRedirectId(String redirectId) {
this.redirectId = redirectId;
}
@Override
public String toString() {
return "userRedirect :: RedirectId: " + redirectId + ", LoopInd : "
+ loopInd + ", Ric_Tgrp Details : " + userRedirectRicTgrpDetailsList;
}
}
userRedirectRicTgrpDetails.java
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.QueryHint;
import javax.persistence.Table;
import org.codehaus.jackson.annotate.JsonProperty;
@Entity
@Cacheable(false)
@NamedQueries({
@NamedQuery(name = "getuserRedirectDetailsById", query = "SELECT a FROM userRedirectRicTgrpDetails a WHERE a.userRedirectEntity.redirectId=:redirectId ORDER BY a.priority", hints = @QueryHint(name = "eclipselink.refresh", value = "true"))
,
@NamedQuery(name = "getAlluserRedirectDetails", query = "SELECT a FROM userRedirectRicTgrpDetails a ORDER BY a.priority", hints = @QueryHint(name = "eclipselink.refresh", value = "true"))})
@Table(name = "userRedirect_ric_tgrp_details")
public class userRedirectRicTgrpDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "row_id")
private int id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "redirect_id")
private userRedirect userRedirectEntity;
@Column(name = "ric")
@JsonProperty("ric")
private String ric;
@Column(name = "tgrp")
@JsonProperty("tgrp")
private String tgrp;
@Column(name = "priority")
@JsonProperty("priority")
private int priority;
public userRedirectRicTgrpDetails() {
}
public userRedirect getuserRedirect() {
return userRedirectEntity;
}
public void setuserRedirect(userRedirect userRedirect) {
this.userRedirectEntity = userRedirect;
}
/**
* @return the ric
*/
public String getRic() {
return ric;
}
/**
* @param ric the ric to set
*/
public void setRic(String ric) {
this.ric = ric;
}
/**
* @return the tgrp
*/
public String getTgrp() {
return tgrp;
}
/**
* @param tgrp the tgrp to set
*/
public void setTgrp(String tgrp) {
this.tgrp = tgrp;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
@Override
public String toString() {
return "userRedirectRicTgrpDetails ::RowId : " + id + ", RedirectId : " + userRedirectEntity.getRedirectId() + ", Ric : " + ric + ", Tgrp : " + tgrp
+ ", Priority : " + priority;
}
}
例外:
SEVERE: Unexpected error while creating new RedirectGroup Exception =
java.lang.IllegalArgumentException: Object: callRedirect :: RedirectId: 1000, Lo
opInd : ORIG, Ric_Tgrp Details : [callRedirectRicTgrpDetails ::RowId : 0, Redire
ctId : 1000, Ric : RICXXX3, Tgrp : TGRPXXXX3, Priority : 1] is not a known entit
y type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewO
bjectForPersist(UnitOfWorkImpl.java:4184)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(Entity
ManagerImpl.java:368)
数据库架构:
CREATE TABLE IF NOT EXISTS `userRedirect` (
`redirect_id` varchar(20) NOT NULL,
`loop_ind` varchar(4) NOT NULL,
PRIMARY KEY (`redirect_id`)
);
CREATE TABLE IF NOT EXISTS `userRedirect_ric_tgrp_details` (
`row_id` int(4) AUTO_INCREMENT,
`redirect_id` varchar(20) NOT NULL,
`ric` varchar(20) NOT NULL,
`tgrp` varchar (20) NOT NULL,
`priority` int NOT NULL,
PRIMARY KEY (`row_id`)
);
JSON INPUT:
{
"loop_ind": "ORIG",
"callRedirectRicTgrpDetailsList": [{
"ric": "RICXXX3",
"tgrp": "TGRPXXXX3",
"priority": 1
}]
}
并且从URL
提供redirect_id的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="TEST_CALLREDIRECT_PERSISTENCE" transaction-type="RESOURCE_LOCAL">
<class>com.mypackage.userRedirect</class>
<class>com.mypackage.userRedirectRicTgrpDetails</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/CallRedirect" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="password" />
</properties>
</persistence-unit>
</persistence>
DAO.Class:
public userRedirect createNewuserRedirect(userRedirect userRedirect){
try {
System.out.println("inside dao.....");
LOGGER.fine("createNewRedirectGroup method invoked in dao with userRedirect : " + userRedirect);
entityManager.getTransaction().begin();
entityManager.persist(userRedirect);
entityManager.getTransaction().commit();
return userRedirect;
} catch (Exception e) {
System.out.println(e);
}
}