我使用Spring-Boot处理超类型/子类型实体,除了JpaRepository中的默认保存方法外,我已经完成了所有工作。有人可以查看我的代码并发现我的错误吗?谢谢!
Hibernate:
/* insert com.pandera.wilson.model.TEST_Category
*/ insert
into
test_post
(post_body, post_created, descriminator, post_updated)
values
(?, ?, 'category', ?) select
scope_identity()
2016-12-12 15:17:33 WARN SqlExceptionHelper:127 - SQL Error: 0, SQLState: S1093
2016-12-12 15:17:33 ERROR SqlExceptionHelper:129 - The index 4 is out of range.
package com.pandera.wilson.model;
import org.json.JSONException;
import org.json.JSONObject;
import javax.persistence.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Date;
/**
* @author Austin Nicholas
* @category Models
*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="descriminator", discriminatorType = DiscriminatorType.STRING)
@Table(name = "TEST_post")
public abstract class TEST_Post {
@Id
@Column(name = "post_key")
@GeneratedValue(strategy = GenerationType.AUTO)
private long key;
@Lob
@Column(name = "post_Body")
private String body;
@Column(name = "post_Created")
private Timestamp created;
@Column(name = "post_Updated")
private Timestamp updated;
@Column(name = "descriminator")
private String descriminator;
@PrePersist
protected void onCreate() {
Timestamp time = new Timestamp(new Date().getTime());
created = time;
updated = time;
}
@PreUpdate
protected void onUpdate() {
updated = new Timestamp(new Date().getTime());
}
public TEST_Post() {
super();
}
public long getKey() {
return key;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
@Override
public String toString() {
JSONObject ret = new JSONObject();
for (Method m : this.getClass().getMethods()) {
if (m.getName().startsWith("get") && !m.getName().equals("getClass")) {
try {
ret.put(
m.getName().substring(3),
m.invoke(this)
);
} catch (JSONException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
}
}
return ret.toString(2);
}
}
package com.pandera.wilson.model;
import org.json.JSONException;
import org.json.JSONObject;
import javax.persistence.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author Austin Nicholas
* @category Models
*/
@Entity
@DiscriminatorValue("category")
@Table(name = "TEST_category")
public class TEST_Category extends TEST_Post {
@ManyToOne
@JoinColumn(name = "toolkit_key")
private Toolkits toolkit;
@Column(name = "category_title")
private String title;
@Column(name = "category_isVotable")
private boolean isVoteable = false;
public TEST_Category() {
super();
}
public Toolkits getToolkit() {
return toolkit;
}
public void setToolkit(Toolkits toolkit) {
this.toolkit = toolkit;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isVoteable() {
return isVoteable;
}
public void setVoteable(boolean voteable) {
isVoteable = voteable;
}
@Override
public String toString() {
JSONObject ret = new JSONObject();
for (Method m : this.getClass().getMethods()) {
if (m.getName().startsWith("get") && !m.getName().equals("getClass")) {
try {
ret.put(
m.getName().substring(3),
m.invoke(this)
);
} catch (JSONException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
}
}
return ret.toString(2);
}
}
package com.pandera.wilson.repository;
import com.pandera.wilson.model.TEST_Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author Austin Nicholas
* @category Repositories
*/
@Repository
public interface TEST_CommunityRepository extends JpaRepository<TEST_Post, Long> {
}
TEST_Category category = new TEST_Category();
category.setTitle("Testing Title");
category.setToolkit(tk);
category.setBody("Testing body");
// I Autowired the repository
repository.save(category);