已经阅读了很多有关同一问题的问题,但我仍然无法解决此问题。
我需要在数据库上有一个 public void initiateNotification(){
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder = new Notification.Builder(this);
Intent notiIntent = new Intent(this, MainPage.class);
notiIntent.putExtra("fileName", storage.getFileName(Calendar.getInstance()));
notiIntent.setAction("NOTI");
notiIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, notiIntent, 0);
Intent stopIntent = new Intent(this, Services.class);
stopIntent.setAction("STOP");
PendingIntent stopPi = PendingIntent.getService(this, 0, stopIntent, 0);
Notification.Action stopAction = new Notification.Action.Builder(null, "STOP", stopPi).build();
//test
int notifyID = 1;
String CHANNEL_ID = "my_channel_01";// The id of the channel.
CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
Notification noti = new Notification.Builder(Services.this)
.setContentTitle("Well Done! Stay Active!")
.setContentText("Number of Steps : " + mSteps)
.setSmallIcon(R.drawable.bino_small)
.setContentIntent(pi)
.setChannelId(CHANNEL_ID)
.build();
startForeground(1, noti);
}
主键。
String
问题在于,如果我在带有注释的字段import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class MyClass {
@Id
private String myId;
private String name;
// getters and setters..
}
中使用String
类型,则在尝试保存对象时Hibernate会引发异常。
@Id
是的,我正在为该字段设置一个值。
我发现的解决方法:
ids for this class must be manually assigned before calling
注释-不起作用@GeneratedValue
-对我来说不可行Integer
作为参数myId
的构造函数-不起作用这些变通办法都不适合我。
更新
我正在使用Spring Boot和Spring Data JPA。
如何插入:
我有一个带注释的public MyClass(String myId){ ... }
方法,该方法处理POST请求并调用执行一些业务逻辑的服务,并调用我的存储库以进行持久化。
我发布的请求:
@PostMapping
MyService.java
{
"myId": "myId",
"name": "myName"
}
答案 0 :(得分:0)
尝试这种方法
@Entity
public class MyClass{
@Id
@GeneratedValue(generator = “UUID”)
@GenericGenerator(
name = “UUID”,
strategy = “org.hibernate.id.UUIDGenerator”,
)
@Column(name = “id”, updatable = false, nullable = false)
private UUID id;
…
}
====================================
我这样调用,并且在我的环境中一切正常:
@Autowired
private EntityManager entityManager;
@PostMapping("")
@Transactional
public void add(@RequestBody MyClass myClass){
entityManager.persist(myClass);
}
并要求通过邮件发送正文:
{
"myId" : "213b2bbb1"
}