gpt4 book ai didi

java - 通过 hibernate 删除对象并且失败

转载 作者:行者123 更新时间:2023-12-02 06:56:25 29 4
gpt4 key购买 nike

我无法直接使用 REmover 类删除代码对象...并且它给了我一个错误...但是当我第一次删除用我的代码添加的注释然后删除代码时,它就完成了......还有其他方法可以自动删除代码和代码对象的注释吗?关于...

WARN: SQL Error: 0, SQLState: 23503 Jun 24, 2013 2:25:51 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: ERROR: update or delete on table "code" violates foreign key constraint "fk_evs9qdyt93ms7uvl3ud2nwy71" on table "comments" Detail: Key (codeid)=(14) is still referenced from table "comments". org.hibernate.exception.ConstraintViolationException: could not execute statement at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)

@Entity
public class Userdetails{
@Id
@GeneratedValue
private long userid;
@Column(nullable = false, unique = true)
private String username;
private String password;
private String fname;
private String lname;
private String email;
@OneToMany(targetEntity=Code.class, mappedBy="userdetails")
private List<Code> codes;
@OneToMany(targetEntity=Messages.class, mappedBy="SMS")
private List<Messages> messages;

public List<Messages> getMessages() {
return messages;
}
public void setMessages(List<Messages> messages) {
this.messages = messages;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

public List<Code> getCodes() {
return codes;
}
public void setCodes(List<Code> codes) {
this.codes = codes;
}

public Userdetails(String username, String password, String fname, String lname, String email) {
super();
this.username = username;
this.password = password;
this.fname = fname;
this.lname = lname;
this.email = email;
}
public Userdetails() {
super();
}
public static boolean updateUserDetails(Object current) {
return UserDBHandler.updateObject(current);
}
public static boolean setUserDetails(Userdetails user) {
return UserDBHandler.saveObject(user);
}
public static Userdetails getUserDetails(String username, String password) {
return UserDBHandler.loadUser(username, password);
}
public static boolean isUser(String username,String passw0rd) {
return UserDBHandler.isUser(username,passw0rd);
}
public static boolean LogoutUser() {
return UserDBHandler.LogoutUser();
}


}



package UsersProperty;

@Entity
public class Code {
@Id
@GeneratedValue
private int codeId;
private int Rating;
private int NORaters;
@Column(nullable = false, unique = true)
private String fileName;
private String OwnerName;
private String PrjName;
private String CodeLang;
private String Tags;
private String Tags2;
@ManyToOne()
@JoinColumn(name="userid")
private Userdetails userdetails;
@OneToMany(targetEntity=Comments.class, mappedBy="targetCode")
private List<Comments> cm;

public int getCodeId() {
return codeId;
}
public void setCodeId(int codeId) {
this.codeId = codeId;
}
public int getNORaters() {
return NORaters;
}
public void setNORaters(int nORaters) {
NORaters = nORaters;
}
public int getRating() {
return Rating;
}
public void setRating(int rating) {
Rating = rating;
}

public List<Comments> getCm() {
return cm;
}
public void setCm(List<Comments> cm) {
this.cm = cm;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getOwnerName() {
return OwnerName;
}
public void setOwnerName(String ownerName) {
OwnerName = ownerName;
}

public String getPrjName() {
return PrjName;
}
public void setPrjName(String prjName) {
PrjName = prjName;
}

public String getCodeLang() {
return CodeLang;
}
public void setCodeLang(String codeLang) {
CodeLang = codeLang;
}
public String getTags() {
return Tags;
}
public void setTags(String tags) {
Tags = tags;
}
public String getTags2() {
return Tags2;
}
public void setTags2(String tags2) {
Tags2 = tags2;
}
public Userdetails getUserdetails() {
return userdetails;
}
public void setUserdetails(Userdetails userdetails) {
this.userdetails = userdetails;
}
public Code(String fileName, String ownerName, String prjName,String Lang,String tAgs,String tAgs2) {
super();
this.fileName = fileName;
OwnerName = ownerName;
PrjName = prjName;
Tags=tAgs;
Tags2=tAgs2;
CodeLang = Lang;
}
public Code() {
super();
}

public static void CodeSearch(String TAGS){
SpecificCodeSearchDBHandler.searchCodeTags(TAGS);
}
public static Code LoadCode(String Path){
return CodePathLoaderDBHandler.loadCode(Path);
}
public static String FNameExtractor(String FilePath){
String Fname = null;
@SuppressWarnings("resource")
Scanner Reader = new Scanner(FilePath);
Reader.useDelimiter("/");
while(Reader.hasNext())
Fname = Reader.next();
return Fname;

}
}



package UsersProperty;


@Entity
public class Comments {
@Id
@GeneratedValue
private int cmId;
private String Cm;
private String Cmplacer;
@ManyToOne()
@JoinColumn(name="codeId")
private Code targetCode;

public Comments() {
}

public String getCm() {
return Cm;
}
public void setCm(String cm) {
Cm = cm;
}
public String getCmplacer() {
return Cmplacer;
}
public void setCmplacer(String cmplacer) {
Cmplacer = cmplacer;
}
public Code getTargetCode() {
return targetCode;
}
public void setTargetCode(Code targetCode) {
this.targetCode = targetCode;
}
public Comments(String cm, String cmplacer) {
super();
Cm = cm;
Cmplacer = cmplacer;
}

}




public class REmover {
private static Session scss;
private static SessionFactory sfactory;
static {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass(Userdetails.class);
cfg.addAnnotatedClass(Code.class);
cfg.addAnnotatedClass(Messages.class);
cfg.addAnnotatedClass(Comments.class);
cfg.configure();
new SchemaExport(cfg);
sfactory = cfg.buildSessionFactory();

}
public static boolean removeCode(Object object) {
try {
scss = sfactory.openSession();
scss.beginTransaction();
scss.delete(object);
scss.getTransaction().commit();
return true;
} catch (Exception e) {
e.printStackTrace();
scss.getTransaction().rollback();
}
return false;
}

最佳答案

尝试向您的@OneToMany添加适当的级联操作,例如

@OneToMany(targetEntity=Comments.class, mappedBy="targetCode", cascade=CascadeType.REMOVE)

看看API: http://docs.oracle.com/javaee/6/api/javax/persistence/OneToMany.html#cascade()

和:

http://docs.oracle.com/javaee/6/api/javax/persistence/CascadeType.html

关于java - 通过 hibernate 删除对象并且失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17271643/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com